0

我已经准备好验证号码、电子邮件和消息等。

我现在的问题是如何验证无线电/检查 btn。

这是我验证等消息的方式:

// Validates msg

if (!isset($_POST[$field]) || empty($_POST['message'])) {
    $error_msg .= " ";  
    $fieldStatusMessage = 'style="border:1px solid #F00"';
}
else {
    $fieldStatusEmail = '';
}

如果有人可以解释或向我展示会很有帮助。我认为它真的很接近我现在在做什么?

4

3 回答 3

1

您可以执行以下操作

示例 1

PHP

<?php
  if($_Post[]) {
        if($_POST['list'] == -1)
                echo 'Please select one on the List';
         else {
                ............
         }
}
?>

HTML

<form method="post">
 <select name="list">
         <option value="-1">Select One</option>
         <option value="1">One</option>
         <option value="2">Two</option>
</select>
<input type="submit" name="submit">
</form>

示例 2

HTML

<form name="frm1" action="action.php" method="post">
    Active ? 
    <input type="radio" name="myrdo" value="Y" /> Yes
    <input type="radio" name="myrdo" value="N" /> No
    <br />
    Education:
    <input type="checkbox" name="mychk" value="G" /> Graduate
    <br />
    Country:
    <select name="country">
        <option value="" selected="selected">Select</option>
        <option value="1">USA</option>
        <option value="2">UK</option>
        <option value="3">Canada</option>
    </select>
    <br />
    <input type="submit" name="btnSubmit" value="Submit Form" />
</form>

PHP

if($_SERVER['REQUEST_METHOD'] == 'POST'){ 
    $error_msg = array(); 
    if(!isset($_POST['myrdo'])){ 
        $error_msg[] = "No radio buttons were checked."; 
    } 
    if(!isset($_POST['mychk'])){ 
        $error_msg[] = "Graguate was checked"; 
    } 
    if(!isset($_POST['country'])){ 
        $error_msg[] = "No country as selected."; 
    } 

    if(isset($error_msg) && count($error_msg) == 0){ 
        // do some form processing 
    } 
    else{ 
        // redirect to the form again. 
    } 
}  

阅读更多

于 2013-01-14T13:09:29.283 回答
1

对于复选框,如果用户选中它,您将有一个 $_POST['checkboxName'] 等于“on”。如果用户取消选中它, $_POST['checkboxName'] 将不存在。

对于单选按钮,您将有一个 $_POST['radioName'] 等于所选值(html 属性值)

于 2013-01-14T13:10:22.890 回答
0
// if not checked consider FALSE
$checkbox = (empty($_POST['checkbox_name'])) ? FALSE : TRUE;

// if not selected consider FALSE otherwise use value from HTML.
$radio = (empty($_POST['radio_name'])) ? FALSE : $_POST['radio_name'];
于 2013-01-14T13:16:23.073 回答