mcheng
mcheng

Reputation: 153

How to pass two variable via checkbox value in PHP?

I want to store 2 variables in a value, but I not sure with the syntax. The scenario: I want to store $boothAlias and $day2 into checkbox value to be pass to other page.

<input name='totalDay[]' type='checkbox' value='$boothAlias.$day2'/>

My code segment

while($rows = mysql_fetch_array($result2)){
  $boothAlias=$rows['boothAlias'];
  $totalDay=$rows['totalDay'];
  echo "<tr><td>$boothAlias</td>";
  for ($day2 = 1; $day2 <= $totalDay; ++$day2) {
    echo "<td><input name='totalDay[]' type='checkbox' value='$boothAlias.$day2'/></td>";
  }
  echo "</tr>";
}

Upvotes: 0

Views: 2050

Answers (3)

Your Common Sense
Your Common Sense

Reputation: 157870

<input name='totalDay[$boothAlias]' type='checkbox' value='$day2'/>

Upvotes: 0

donutdan4114
donutdan4114

Reputation: 1272

The easiest thing would be to use some sort of delimeter.

"<td><input name='totalDay[]' type='checkbox' value='$boothAlias|$day2'/></td>";

Notice the "|" delimeter.

Then in your PHP code, to get the two values:

$totalDays = $_POST['totalDay'];
$value = explode("|",$totalDays[0]);
echo $value[0];
echo $value[1];
//this would output
boothAlias_value
day2_value

Not sure if that made total sense, but the idea to store multiple values in a string, is to use a delimeter, and then to convert to an array in PHP.

Upvotes: 2

Bill
Bill

Reputation: 38

I think you would be better off using hidden form fields to pass data like that to your processing script. The checkbox form object is really designed for returning checked/unchecked (true/false). Depending on the value of the checkbox you can process the code and variables accordingly.

Upvotes: 1

Related Questions