如何检查收音机是否被检查? [关闭]

如何检查收音机是否被检查?  [关闭]

问题描述:

How to check if radio button is checked after post?

Code on my radio button

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

code to check my radio:

$Itemtype = $_POST["Itemtype"];

if($_POST["Itemtype"] == "Ingredient")
{
    $try2 = "ingred. working";
]
elseif($_POST["Itemtype"] == "Miscellaneous")
{
    $try2 = "misc. working";
}
else
{
    $errormsg = "error5";
}

If I don't select a radio button it says "syntax error: undefined Index Itemtype" keeps aiming at the $Itemtype = $_POST["Itemtype"];

and if i select radio button it doesn't return checked on the selected radio button.

anyone can give me helping hand on this thanks.

如何检查发布后是否选中单选按钮? p>

代码开启 我的单选按钮 p>

 &lt;?php if(isset($ _ POST ['Itemtype'])&amp;&amp; $ _POST ['Itemtype'] =='Ingredient')  echo'check =“checked”';?&gt; 
&lt;?php if(isset($ _ POST ['Itemtype'])&amp;&amp; $ _POST ['Itemtype'] =='Miscellaneous')echo'trackled =  “检查”';?&gt; 
  code>  pre> 
 
 

检查我的收音机的代码: p>

  $ Itemtype = $ _POST  [“Itemtype”]; 
 
if($ _ POST [“Itemtype”] ==“Ingredient”)
 {
 $ try2 =“ingred.working”; 
] 
elseif($ _ POST [“Itemtype”  ] ==“其他”)
 {
 $ try2 =“misc.hote”; 
} 
else 
 {
 $ errormsg =“error5”; 
} 
  code>  pre  > 
 
 

如果我没有选择单选按钮,则说“语法错误:未定义索引项类型”一直针对 $ Itemtype = $ _POST [“Itemtype”]; code>

如果我选择单选按钮,则不会选中所选单选按钮。 p>

任何人都可以帮助我 谢谢。 p> div>

<?  
    $_POST['Itemtype'] = "Ingredient";
    if(isset($_POST['Itemtype'])) {
        $Itemtype = $_POST["Itemtype"]; 
        if($_POST["Itemtype"] == "Ingredient") {
            $try2 = "ingred. working"; 
        }elseif($_POST["Itemtype"] == "Miscellaneous"){
            $try2 = "misc. working"; 
        } 
    } else { $errormsg = 'error5'; }
    echo $try2;
    echo $errormsg;
?>

I forced the Itemtype for debugging reasons... This would show your logic works... Question I have is are you echoing out your $try2....

You will need to use isset if the radio option wasn't ticked it won't be set.

To check it radio button has been checked (and posted) you can like this

if (isset($_POST['Itemtype'])) { // if ANY radio button was ticked at all
  echo $_POST['Itemtype']; // display the 'value' the choice that was ticked.
} else {
  echo "no radio button ticked.";
}

The 'Itemtype' needs to refer to the name='' field withing your radio button like so.

<input type="radio" name="Itemtype" value="female">Female
<input type="radio" name="Itemtype" value="male">Male

The web browser will only pass the value inside POST if the radio has been checked by the user.

Update your code as follows to check if the radio has been selected:

if(isset($_POST['Itemtype']))
{
    if($_POST["Itemtype"] == "Ingredient")
    { 
        $try2 = "ingred. working";
    }
    elseif($_POST["Itemtype"] == "Miscellaneous")
    {
        $try2 = "misc. working";
    }
}
else
{
    $errormsg = 'error5';
}