Forum

Thread tagged as: Problem

Checkbox checked by default

I really thought that this would be simple, but why does:

<perch:input type="checkbox" id="informed" class="myclass" value="Yes, keep me informed" checked="checked" />

not work ?

I've read a bunch of older posts and have tried value="1", default="1" etc etc as suggested, but still no joy. I am running Perch 2.6.3.

I also realise that the client could be helpful and not want this, but they are adamant they want the checkbox checked by default. Using a radio button would look odd in this instance as well...

Any ideas ?

Andy

Andrew Kennedy

Andrew Kennedy 0 points

  • 7 years ago

If you omit

value="Yes, keep me informed"

the checkbox is pre-checked.
Simply do not specify value. A value of "on" will be submitted through the form.

No need for the

checked="checked"

too.
This is, what it would look like:

<perch:input type="checkbox" id="informed" class="myclass" />
Drew McLellan

Drew McLellan 2638 points
Perch Support

You need to have a value. The 'on' will be coming from some browser trying to patch over the fact that the value is missing.

Set the default attribute to the same as the value to check it.

Okay, so Firfox added that value of 'on'. Thanks for clarifying, Drew.

From your reply Drew, I am assuming you mean that this should work:

<perch:input type="checkbox" id="informed" class="myclass" default="hello" value="hello" />

This however did not allow the checkbox to be checked by default.

The only time I can actually get things working (browsers used are Firefox 31 and IE 11) is using Nils solution:

<perch:input type="checkbox" id="informed" class="myclass" />

This then gives me an 'on' result against the ID if the submitted form still had the checkbox checked, but reading your comment, it seems that default and value should to be used, which makes sense.

Any ideas why it is not working ?

Drew McLellan

Drew McLellan 2638 points
Perch Support

How are you displaying the form?

Hi Drew, sorry for the delay getting back to you.

I am using the forms app and a simple call:

<perch:form id="registration" method="post" app="perch_forms">

Is this what you mean ?

Drew McLellan

Drew McLellan 2638 points
Perch Support

Which page function are you using?

<?php perch_content('Registration Form'); ?>

Drew McLellan

Drew McLellan 2638 points
Perch Support

How about with perch_form() ?

Makes no difference concerning the checkbox, but now no longer see the 'success' message on submission.

Due to me not using a perch_content call anymore.....

Drew McLellan

Drew McLellan 2638 points
Perch Support

Or perch_content_custom() to get the best of both.

Still cannot get the checkbox to be pre-checked.

Ohh well, I will look at trying some alternative solution.

Sorry for really late reply.