Reputation: 13
I found some code to help me with an issue I was having..it is so that if a form does not pass validation, it will retain my checkbox selections. Now I am wondering how do I get the selections in a form that i can put in mysql ?
in my form
<?php $a = array("1*","2*","3*","4*","5*+"); foreach($a as $key => $value) { echo `"<input type='checkbox' name='rating[]' value='$value' `"; if(is_array($_POST['rating']) && in_array($value,$_POST['rating'])) echo " checked "; echo ">$value"; } ?> <INPUT TYPE="SUBMIT" name="submitted" VALUE="Submit" class="submit"> </form> </code>
Upvotes: 1
Views: 849
Reputation: 9328
Is this what you were asking for?
foreach($_POST['rating'] as $checked_value)
{
//do what you want to do with here like INSERT or UPDATE
echo $checked_value.'<br />';
}
EDIT: Perhaps too this will help if you are looking to do arithmetic or analysis on the ratings
$a = array( "1" => "1*", "2" => "2*", "3" => "3*", "4" => "4*", "5" => "5*+");
foreach($a as $key => $value)
{
echo '<input type="checkbox" name="rating[]" value="'.$key.'"';
/* replaced
if(is_array($_POST['rating']) && in_array($value,$_POST['rating']))
with below */
if(is_array($_POST['rating']) && in_array($key,$_POST['rating']))
echo " checked ";
echo " />$value";
}
Not sure as to your implementation of this code but are you sure a checkbox is the correct input type? Perhaps a radio button would be better as users could only choose 1 rating. As it is currently set up, a user could choose 1, 3, and 5 as their rating.
Upvotes: 2