如何检查单选按钮并获取值

时间:2014-03-11 03:27:54

标签: php radio-button

如何在提交后检查单选按钮以及如何获取单选按钮的值?

这是我目前的代码

    <input type="radio" name="Itemtype" id="Ingredient" value="Ingredient" <?php if(isset($_POST['Itemtype'])) echo "Checked"; ?>  >
Ingredient
<input type="radio" name="Itemtype" id="Miscellaneous" value="Miscellaneous" <?php if(isset($_POST['Itemtype'])) echo "Checked"; ?> >
Miscellaneous

也尝试使用此功能,但最终结果是语法错误&amp;&amp;

<?php if(isset($_POST['Itemtype'])) && $_POST['Itemtype'] == 'Ingredient') echo 'checked="checked" ';?>
 <?php if(isset($_POST['Itemtype'])) && $_POST['Itemtype'] == 'Miscellaneous') echo 'checked="checked" ';?>

1 个答案:

答案 0 :(得分:0)

You can try this(there is one extra ) remove that ):

<?php if(isset($_POST['Itemtype']) && $_POST['Itemtype'] == 'Ingredient') echo 'checked="checked" ';?>
<?php if(isset($_POST['Itemtype']) && $_POST['Itemtype'] == 'Miscellaneous') echo 'checked="checked"';?>

或者您可以简单地写一下:

<?php if($_POST['Itemtype'] == 'Ingredient') echo 'checked="checked" ';?>
<?php if($_POST['Itemtype'] == 'Miscellaneous') echo 'checked="checked"';?>