Marcos01 Posted November 30, 2008 Share Posted November 30, 2008 Hello, I want to store multiple checkbox values from 1 question. It saves only one value. Checkbox name:sprintf("%02d%02d", $i,$j) value: $_POST[sprintf("%02d%02d", $i,$j)] I can't figure it out. Thanks for your help. for ($i=1; $i <= $highpage; $i ++) { for ($j=1; $j <= $maxquestions ; $j ++) { if ( isset($_POST[sprintf("%02d%02d", $i,$j)])) { $query = "INSERT INTO `bkdb`.`formanswers` (page , questionnumber, responsenum, sessionid) values('$i','$j','".$_POST[sprintf("%02d%02d", $i,$j)]."','$sessionid')"; echo "<p>$query</p>"; mysql_query($query, $bkdb) or die("Could not save last page answers"); } } } Quote Link to comment https://forums.phpfreaks.com/topic/134875-saving-mulitple-checkbox-values/ Share on other sites More sharing options...
.josh Posted November 30, 2008 Share Posted November 30, 2008 <?php if ($_POST['fruit']) { // example1: dump the array print_r($_POST['fruit']; // example2: individual naming. If all 3 were checked, each of these vars would exist echo $_POST['fruit'][0]; echo $_POST['fruit'][1]; echo $_POST['fruit'][2]; // example3: loop through it foreach ($_POST['fruit'] as $fruit) { echo "$fruit <br />"; } // end foreach } // end for ?> <form action = '' method = 'post'> <input type = 'checkbox' name='fruit[]' value='apple'>apple <br /> <input type = 'checkbox' name='fruit[]' value='orange'>orange <br /> <input type = 'checkbox' name='fruit[]' value='banana'>banana <br /> <input type = 'submit' value = 'submit'> </form> Dunno how you're storing the choices in the database, but if you're looking for the answers to be in a single column as a string or whatever, you can implode $_POST['fruit']. Otherwise, you'll have to create another loop inside your loops to go through each element in $_POST['fruit'] Quote Link to comment https://forums.phpfreaks.com/topic/134875-saving-mulitple-checkbox-values/#findComment-702257 Share on other sites More sharing options...
Marcos01 Posted November 30, 2008 Author Share Posted November 30, 2008 Crayon Violent, thanks for your reply I would like to do it with a loop. However I am not so good with php so how do I handle that? Quote Link to comment https://forums.phpfreaks.com/topic/134875-saving-mulitple-checkbox-values/#findComment-702263 Share on other sites More sharing options...
.josh Posted November 30, 2008 Share Posted November 30, 2008 Look at example3 in the code I posted. Quote Link to comment https://forums.phpfreaks.com/topic/134875-saving-mulitple-checkbox-values/#findComment-702264 Share on other sites More sharing options...
Marcos01 Posted November 30, 2008 Author Share Posted November 30, 2008 When I try this foreach ($_POST[sprintf("%02d%02d", $i,$j)] as $test) { echo "$test <br />"; } I get this error: Warning: Invalid argument supplied for foreach() Quote Link to comment https://forums.phpfreaks.com/topic/134875-saving-mulitple-checkbox-values/#findComment-702274 Share on other sites More sharing options...
Marcos01 Posted November 30, 2008 Author Share Posted November 30, 2008 Does anyone know how to deal with this? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/134875-saving-mulitple-checkbox-values/#findComment-702450 Share on other sites More sharing options...
.josh Posted November 30, 2008 Share Posted November 30, 2008 show your form. I'm having trouble trying to figure out why you're using that sprintf. Quote Link to comment https://forums.phpfreaks.com/topic/134875-saving-mulitple-checkbox-values/#findComment-702618 Share on other sites More sharing options...
Marcos01 Posted November 30, 2008 Author Share Posted November 30, 2008 Crayon Violent, thanks for looking into this. Form is a bit long. I limited to the input. I hope that is enough. echo '<input type="'.$row2['type'].'" name="'.sprintf("%02d%02d",$row2['page'], $row2['questionnumber']).'" value="'.$row2['responsenum'].'"'; $currentquestion = $row2['questionnumber']; $query = "SELECT * from formanswers where page='$currentpage' and sessionid='$sessionid' and questionnumber='$currentquestion' ORDER BY questionnumber"; $result = mysql_query($query, $bkdb) or die("current page could not be found"); $row = mysql_fetch_array($result); $CUR_QNUM = $row['questionnumber']; $CUR_RNUM = $row['responsenum']; if ($row2['questionnumber']==$CUR_QNUM && $row2['responsenum']==$CUR_RNUM) { echo "checked"; $row = mysql_fetch_array($result); } echo ' />'; Quote Link to comment https://forums.phpfreaks.com/topic/134875-saving-mulitple-checkbox-values/#findComment-702639 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.