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 ?

 
JohntheFish replied on at Permalink Reply
JohntheFish
There can be some funnies about the names of data coming in to php (its a php thing).

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.
echo "<pre>";
echo "\n\n\n\n********************\n";
echo htmlentities(print_r($_POST,true));
echo "\n====================";
echo "</pre>";


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.
robinh replied on at Permalink Reply
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
JohntheFish replied on at Permalink Reply
JohntheFish
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)
robinh replied on at Permalink 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)."')");
         }
JohntheFish replied on at Permalink Reply
JohntheFish
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.
JohntheFish replied on at Permalink Reply
JohntheFish
Also relating to (1), check the form posted by the browser in the network tab of the browser developer console.
robinh replied on at Permalink Reply
$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.
JohntheFish replied on at Permalink Reply
JohntheFish
Can you explain why you are setting and saving $args from some of the values in $data?
robinh replied on at Permalink Reply
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 )
JohntheFish replied on at Permalink Reply
JohntheFish
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.
JohntheFish replied on at Permalink Reply
JohntheFish
For the checkbox test, you could try:
if (isset($data['update']) && $data['update']=='check'){
....
}
robinh replied on at Permalink Reply
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 :)
JohntheFish replied on at Permalink Best Answer Reply
JohntheFish
First try coding the checkbox directly
<input type="checkbox" name="update" value="check"> Update
robinh replied on at Permalink Reply
Yup, that did it... do you have any idea why this would work but one 'generated' by the form helper not ?
JohntheFish replied on at Permalink Reply
JohntheFish
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.
robinh replied on at Permalink Reply
I got back to the problem and added the $form->checkbox and it worked... The only thing i changed was the name.