Welcome Guest, Not a member yet? Register   Sign In
Checkbox Help needed
#1

[eluser]K-Fella[/eluser]
Hi folks,

I need your help figuring this one out :ohh:

What I'm trying to do is have a checkbox ticked upon page load (before the form is submitted), but if the checkbox is unticked by the user and then the form is submitted, to show an unticked checkbox on the resulting page load.

My problem is, in order to tick the checkbox on the first page load I need to make sure no post data for that checkbox exists. If it doesn't I tell the form_checkbox() function to tick the box.
Code:
if (!isset($this->post->input['allow_email']))
{
echo form_checkbox('allow_email', 'on', TRUE);
}
But, if the box is unticked by the user and then they submit the form, no post data for the box exists on the resulting page load, so my if statement above kicks in and the checkbox is ticked again Undecided

What can I do?

The use of the set_checkbox() function won't help in this case. The only solution I can see working is to use javascript to set a hidden field containing a value of 'off' or 0 if the box is unticked, but if javascript is off in the browser I'm screwed!
#2

[eluser]coolfactor[/eluser]
I'm assuming you output the checkbox whether it's ticked or not, but your code doesn't show that. Try this instead:

Code:
echo form_checkbox('allow_email', 'on', $this->post->input('allow_email'));

You want to be controlling that third parameter, rather than have it fixed.
#3

[eluser]Luci3n[/eluser]
I wrote a turtorial about this check it out

Extending the Validator Class to set default values

hope this helps
#4

[eluser]obiron2[/eluser]
I am sure that form submission does not send the form field for a checkbox unless it is checked.

You need to modify your function so that if the form has been submitted and the field was not in $_POST then you can assume it was unchecked on submission. If the form has never been submitted then $_POST will not contain the remaining fields and you can default the checkbox to on.

Incidentally, if you are in the UK and the checkbox is a permission to mail / use personal details then this box should default to unchecked. Permission to mail/email/rent is an opt-in basis under the data protection act.
#5

[eluser]K-Fella[/eluser]
[quote author="obiron2" date="1188317173"]Incidentally, if you are in the UK and the checkbox is a permission to mail / use personal details then this box should default to unchecked. Permission to mail/email/rent is an opt-in basis under the data protection act.[/quote]I'm in Ireland so I'm not sure if this applies, but for the sake of compliancy I think I will leave it as opt-in and not opt-out.

Thanks to everyone for the responses Smile




Theme © iAndrew 2016 - Forum software by © MyBB