Forum Moderators: coopster

Message Too Old, No Replies

POST value to checkbox input FAILS

         

kristo5747

9:30 pm on Apr 9, 2010 (gmt 0)

10+ Year Member



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?

kristo5747

10:06 pm on Apr 9, 2010 (gmt 0)

10+ Year Member



Never mind.

Here's the solution...

...<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>...


Seems that my prob was that I evaluated if the posted checkbox array is equal to a string. Serialize() does not seem to do the job here.................

Readie

10:13 pm on Apr 9, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



OK, there's an easier way to do this, well, I say easier, but there's a more convenient way anyway:

<?php

$check_values = array(
'CSV',
'Excel',
'Email',
'HTML',
'None',
'PDF'
);
$count = count($check_values);

echo '<fieldset id="output"><legend>Output Options: </legend>';
for($i = 0; $i < $count; $i++) {
if(sset($_POST['check_output']) && in_array($check_values[$i], $_POST['check_output'])) {
$che = ' checked';
} else {
$che = '';
}
echo "\n" . '<input type="checkbox" tabindex="' . ($i + 2) . '" id="chb' . $i . '" name="check_output[]"' . $che . '>' . $check_values[$i] . '<br>';
}
echo "\n" . '</fieldset>'

?>

Welcome to Webmaster World by the way, sorry I've not been on today to help you, but I'm glad you found a solution :)

kristo5747

10:47 pm on Apr 9, 2010 (gmt 0)

10+ Year Member



No need to apologize. Thanks for taking the time!

:-)