checkbox always returns 1
Permalink
Hi,
The title explains everything. i got 1 checkbox and overrided the save function from the controller. for some reason when i submit it will always post 1 even if the checkbox isnt checked.
echo $form->checkbox('update', 'check', false);
i guessed it would send the string 'check' when it was on and wouldnt be send at all if i left it unchecked but what it does is return 1
any thoughts ?
The title explains everything. i got 1 checkbox and overrided the save function from the controller. for some reason when i submit it will always post 1 even if the checkbox isnt checked.
echo $form->checkbox('update', 'check', false);
i guessed it would send the string 'check' when it was on and wouldnt be send at all if i left it unchecked but what it does is return 1
any thoughts ?
I used your code and it returned an empty array, with $data which i use in my save function i get nothing. i do put the data in a db which fills with a 1.
if you have any ideas left i'm listing
if you have any ideas left i'm listing
Can you post your save function here. Put it inside code /code (see the note about code sample below the message text box of Post Reply)
public function save($data){ if (isset($data['update'])) { $db = loader::db(); $mails = $this->getAllMail(); //delete everything before entering new data to overcome duplicates. $db->Execute("TRUNCATE TABLE btmailbox"); foreach ($mails as $mail){ $formatted = date('Y-m-j', strtotime($mail->date)); $db->Execute("INSERT INTO btmailbox (`title`, `from`, `to`, `date`) VALUES ('".mysql_real_escape_string($mail->subject)."', '".mysql_real_escape_string($mail->from)."', '".mysql_real_escape_string($mail->to)."', '".mysql_real_escape_string($formatted)."')"); }
Viewing 15 lines of 22 lines. View entire code block.
A couple of things to check:
1. If $data is always dumping as empty/null, do you have the form wrapped in <form> and </form> tags?
2. Your save function is reading from $data, but saving $args. So anything in $data that is not explicitly copied to $args will keep whatever default value the block has.
1. If $data is always dumping as empty/null, do you have the form wrapped in <form> and </form> tags?
2. Your save function is reading from $data, but saving $args. So anything in $data that is not explicitly copied to $args will keep whatever default value the block has.
Also relating to (1), check the form posted by the browser in the network tab of the browser developer console.
$data returns every value and saves it in the db. the $data[update] only for checking. it doesnt need to go into a db. everything in $data is fine except the checkbox which always returns 1. i looked in the chrome dev tool and it also says its a 1.
Can you explain why you are setting and saving $args from some of the values in $data?
it was from an example. As for as i know not any particulare reason... but if i out $data in the parent::save() then i would break i guess cause it would get 5 values but only have 4 db columns to put them ( the value from the checkbox doesnt need to be saved in a db )
The array passed to parent::save is saved by matching named elements to database columns, so there is no problem if it contains spurious elements unless their names match columns for that table.
If you parent::save($args), then only what you have set in $args will be saved. Any other values in $data that the block needs will be ignored. Maybe that is what you want. Or maybe something important could be lost.
If you parent::save($args), then only what you have set in $args will be saved. Any other values in $data that the block needs will be ignored. Maybe that is what you want. Or maybe something important could be lost.
For the checkbox test, you could try:
if (isset($data['update']) && $data['update']=='check'){ .... }
i tried that but then it doesnt work because it for some weird reason only returns 1 the vlaue isnt send.. i have decided that i will use radio buttons ( they do work which makes it even stranger) thanks for your help and i probably will try to fix this an other time but i have to set priority's and 2 days work on a single element is a bit much :)
First try coding the checkbox directly
<input type="checkbox" name="update" value="check"> Update
Yup, that did it... do you have any idea why this would work but one 'generated' by the form helper not ?
The form helper tries to maintain form state, so perhaps that had an impact on it. I use my own hacked version with an option to turn state persistence off.
To get to the bottom of it, you would have to put both the form helper version and the direct version on a page and compare the html generated over a series of uses of the page.
To get to the bottom of it, you would have to put both the form helper version and the direct version on a page and compare the html generated over a series of uses of the page.
I got back to the problem and added the $form->checkbox and it worked... The only thing i changed was the name.
To see what is actually sent from the form, you can use the browser developer console network tab.
To see what is actually coming in to php, I often use variations of this code in my save or at the top of the view method of the controller.
All the new lines are to get it below the dashboard bar. You can also change $_POST to $_GET or $data.
I also have a free addon that may help with diagnostics:
http://www.concrete5.org/marketplace/addons/quick-param-view/...
(It incorporates variations of the above code)
Finally, the standard form helper has issues with data persistence between submissions. Sometimes you want it, but especially for checkboxes the persistence can be buggy. Personally I use a hacked version of the form helper that has an option to disable data persistence.