Jump to content

Archived

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

slanton

Retaining a dynamic checkbox value

Recommended Posts

I have a check box that is dispayed using
[code]type='checkbox' name='book[$rm][]' value='$d' [/code]
I can get the values using
foreach ($_POST['book'] as $rm => $dates) { etc
but how can I retain selected values when validating

Share this post


Link to post
Share on other sites
$checked = in_array($d, $_POST['book'][$rm]) ? 'checked' : '';

echo "<input type='checkbox' name='book[$rm][]' value='$d' $checked>";

Share this post


Link to post
Share on other sites
Thanks. I tried that and got the following error message.
Warning: in_array(): Wrong datatype for second argument in...etc

Share this post


Link to post
Share on other sites
Then $_POST['book'][$rm] isn't an array. Data not posted yet?

[code]if (isset($_POST['book'][$rm]) && is_array($_POST['book'][$rm])) {
     $checked = in_array($d, $_POST['book'][$rm]) ? 'checked' : '';
}
else $checked = '';

echo "<input type='checkbox' name='book[$rm][]' value='$d' $checked>";[/code]

Share this post


Link to post
Share on other sites
I tried that and I no longer get the error message about wrong datatype but I still can't seem to get this to work.
I am definitely getting an array as I have done [code]<?php print_r($_POST['book'][$rm]); ?>[/code] and get values from the selected checkboxes.
To test it and see what is going on,I have also echoed $checked but get nothing.
If I put
[code] $checked = in_array($d, $_POST['book'][$rm]) ? 'checked' : 'No';[/code] and echo $checked I get the word "No". So I think the problem is in the value of $d as $d is not in the array even though the array has values.

Share this post


Link to post
Share on other sites

×

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.