slanton Posted June 5, 2006 Share Posted June 5, 2006 I have a check box that is dispayed using [code]type='checkbox' name='book[$rm][]' value='$d' [/code]I can get the values usingforeach ($_POST['book'] as $rm => $dates) { etcbut how can I retain selected values when validating Quote Link to comment https://forums.phpfreaks.com/topic/11279-retaining-a-dynamic-checkbox-value/ Share on other sites More sharing options...
Barand Posted June 5, 2006 Share Posted June 5, 2006 $checked = in_array($d, $_POST['book'][$rm]) ? 'checked' : '';echo "<input type='checkbox' name='book[$rm][]' value='$d' $checked>"; Quote Link to comment https://forums.phpfreaks.com/topic/11279-retaining-a-dynamic-checkbox-value/#findComment-42217 Share on other sites More sharing options...
slanton Posted June 6, 2006 Author Share Posted June 6, 2006 Thanks. I tried that and got the following error message.Warning: in_array(): Wrong datatype for second argument in...etc Quote Link to comment https://forums.phpfreaks.com/topic/11279-retaining-a-dynamic-checkbox-value/#findComment-42246 Share on other sites More sharing options...
Barand Posted June 6, 2006 Share Posted June 6, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/11279-retaining-a-dynamic-checkbox-value/#findComment-42318 Share on other sites More sharing options...
slanton Posted June 6, 2006 Author Share Posted June 6, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/11279-retaining-a-dynamic-checkbox-value/#findComment-42331 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.