Jump to content

POST value to checkbox input FAILS


kristo5747

Recommended Posts

Hello!

 

PHP moron here (surely feel like one today, anyway).

 

I'm coding a page form wherein I have to echo the POST values back into their respective form fields to prevent their reset. My problem with an array of check-boxes. Echo-ing the POST values back does not work!  Here's my code

 

<td><fieldset id="output"><legend>Output Options: </legend>
<input type="checkbox" tabindex="2" id="chb0" name="check_output[]" value="CSV" <?php echo (serialize($_POST['check_output'])=="CSV")?"CHECKED":""; ?>>CSV<br>
<input type="checkbox" tabindex="3" id="chb1" name="check_output[]" value="Excel" <?php echo (serialize($_POST['check_output'])=="Excel")?"CHECKED":""; ?>>Excel<br>
<input type="checkbox" tabindex="4" id="chb2" name="check_output[]" value="Email" <?php echo (serialize($_POST['check_output'])=="Email")?"CHECKED":""; ?>>Email<br>
<input type="checkbox" tabindex="5" id="chb3" name="check_output[]" value="HTML" <?php echo (serialize($_POST['check_output'])=="HTML")?"CHECKED":""; ?>>HTML<br>
<input type="checkbox" tabindex="6" id="chb4" name="check_output[]" value="None" <?php echo (serialize($_POST['check_output'])=="None")?"CHECKED":""; ?>>None<br>
<input type="checkbox" tabindex="7" id="chb5" name="check_output[]" value="PDF" <?php echo (serialize($_POST['check_output'])=="PDF")?"CHECKED":""; ?>>PDF<br>
</fieldset></td>

 

Since I need to treat my group of checkboxes as an array, I use serialized() to grab the selected value for the checkbox that was clicked.

 

The form is successfully submitted and the data correctly captured in my db but all checkboxes get reset anyway.

 

What I am doing wrong>? :wtf:

Link to comment
https://forums.phpfreaks.com/topic/198165-post-value-to-checkbox-input-fails/
Share on other sites

You're trying to check if the posted checkbox array is equal to a string, which it can't be, change it so it's like this:

 

<input type="checkbox" tabindex="2" id="chb0" name="check_output[]" value="CSV" <?php if(isset($_POST['check_output']) && in_array('CSV', $_POST['check_output'])) echo ' checked ';?>>CSV<br>

You're trying to check if the posted checkbox array is equal to a string, which it can't be...

 

I implemented your suggestion. Works like a charm. Thank you.

 

Did I misunderstand the purpose of serialize() then???

 

serialize can't be used in the way you tried to use it. Lets say all of the checkboxes were ticked then the contents of $_POST['check_output'] would be the same as this array:

 

array('CSV','Excel','Email','HTML','None','PDF');

 

So if you try to serialize that array the result is this string:

 

a:6:{i:0;s:3:"CSV";i:1;s:5:"Excel";i:2;s:5:"Email";i:3;s:4:"HTML";i:4;s:4:"None";i:5;s:3:"PDF";}

 

You were then comparing the result of serialize with "PDF" or "None" etc etc. So that would be essentially saying

 

if(  "PDF"  ==  "a:6:{i:0;s:3:"CSV";i:1;s:5:"Excel";i:2;s:5:"Email";i:3;s:4:"HTML";i:4;s:4:"None";i:5;s:3:"PDF";}"    )

 

As you can see the result of your serialize call can never equal "PDF" because serialize basically converts and encodes an array into a string.

 

 

[quote author=the182guy

 

...................

serialize can't be used in the way you tried to use it. Lets say all of the checkboxes were ticked then the contents of $_POST['check_output'] would be the same as this array:

 

array('CSV','Excel','Email','HTML','None','PDF');

 

So if you try to serialize that array the result is this string:

 

a:6:{i:0;s:3:"CSV";i:1;s:5:"Excel";i:2;s:5:"Email";i:3;s:4:"HTML";i:4;s:4:"None";i:5;s:3:"PDF";}

 

You were then comparing the result of serialize with "PDF" or "None" etc etc. So that would be essentially saying

 

if(  "PDF"  ==  "a:6:{i:0;s:3:"CSV";i:1;s:5:"Excel";i:2;s:5:"Email";i:3;s:4:"HTML";i:4;s:4:"None";i:5;s:3:"PDF";}"    )

 

As you can see the result of your serialize call can never equal "PDF" because serialize basically converts and encodes an array into a string.

 

Makes sense!!

 

Thanks for taking the time. :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.