Jump to content

saving mulitple checkbox values


Marcos01

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/134875-saving-mulitple-checkbox-values/
Share on other sites

<?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']

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 ' />';


Archived

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

×
×
  • Create New...

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.