有人能告诉我为什么我的部分代码没有显示吗?
这是我的HTML表单:
<form action="OrderOutput.php" method="Post">
<fieldset>
<legend>Select a Crust</legend>
<table>
<tr>
<td>
<input type="radio" name="choice" value="Thin">Thin
</td>
<td>
$1.00
</td>
</tr>
<tr>
<td>
<input type="radio" name="choice" value="Medium">Medium
</td>
<td>
$2.00
</td>
</tr>
<tr>
<td>
<input type="radio" name="choice" value="Thick">Thick
</td>
<td>
$3.00
</td>
</tr>
</table>
</fieldset>
<fieldset>
<legend>Select Toppings</legend>
<table>
<tr>
<td>
<input type="checkbox" name="check[]" value="Mushrooms">Mushrooms
</td>
<td>
$4.00
</td>
</tr>
<tr>
<td>
<input type="checkbox" name="check[]" value="Pepperoni">Pepperoni
</td>
<td>
$5.00
</td>
</tr>
</table>
</fieldset>
<input type="submit" name="checkout" value="Checkout">
</form>
当我将值发送到php页面时,它只显示以下行:
$selected_crust = $_POST['choice'];
echo "Your selections: Crust: $selected_crust";
但不是这个:
foreach ($_POST['check'] as $selected_toppings) {
echo "Toppings: $selected_toppings ";
}
我的html复选框是否有问题,导致信息无法在php中显示?
您的代码工作正常,您只需要了解用户是否没有提交复选框:
if ( isset($_POST['check']) && is_array($_POST['check']) )
{
//If you want to just show toppings once, a simple implode will work:
//echo "Toppings: ", implode(', ', $selected_toppings);
foreach ($_POST['check'] as $selected_toppings) {
echo "Toppings: $selected_toppings ";
}
}
else
{
echo "Toppings: You didn't select any!";
}