Jump to content


Photo

Retaining a dynamic checkbox value


  • Please log in to reply
4 replies to this topic

#1 slanton

slanton
  • Members
  • PipPip
  • Member
  • 27 posts
  • LocationNew Zealand

Posted 05 June 2006 - 10:50 PM

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


#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 05 June 2006 - 11:04 PM

$checked = in_array($d, $_POST['book'][$rm]) ? 'checked' : '';

echo "<input type='checkbox' name='book[$rm][]' value='$d' $checked>";
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 slanton

slanton
  • Members
  • PipPip
  • Member
  • 27 posts
  • LocationNew Zealand

Posted 06 June 2006 - 12:50 AM

Thanks. I tried that and got the following error message.
Warning: in_array(): Wrong datatype for second argument in...etc


#4 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 06 June 2006 - 06:32 AM

Then $_POST['book'][$rm] isn't an array. Data not posted yet?

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>";

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#5 slanton

slanton
  • Members
  • PipPip
  • Member
  • 27 posts
  • LocationNew Zealand

Posted 06 June 2006 - 08:25 AM

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
<?php print_r($_POST['book'][$rm]); ?>
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
$checked = in_array($d, $_POST['book'][$rm]) ? 'checked' : 'No';
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.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users