dtkago3208 2013-07-31 15:21
浏览 64

在php中验证单选按钮

I'm new to PHP and trying to write code to test whether or not a user has clicked a radio button in response to a survey question. There are numerous radio buttons. If they haven't clicked on one then I'd like to issue an error to the user. I've tried a couple of approaches, but haven't found anything that works. Here is my current code and the error message I get. For the PHP script, I've tried all of the three following examples:

    ....

    if ($_POST['degree_type'] == "MS"||"MA"||"MBA"||"JD"||"PhD") {
        $degree_type = ($_POST['degree_type']); 
    } else if ($_POST['degree_type'] == null) {
        $errors[] = 'Please select a degree type.';
    }

    if (isset($_POST['degree_type'])) {
      $errors[] = 'Please select a degree type.';
    } else {
      $degree_type= $_POST['degree_type'];
    }

   if (array_key_exists('degree_type', $_POST)) {
      $degree_type = ($_POST['degree_type']); 
    } else {
      $errors[] = 'Please select a degree type.';
    }
    ....

Here is my html, located in the same page and below the PHP.

          <table>
            <tr>
              <td class="span6">What type of degree?</td>
              <td class="span6">
                      <input type="radio" name="degree_type" value="MA"
                        <?php if (($_POST['degree_type']) == 'MA') {echo 'checked="checked"';} ?>
                      >MA
                      <input type="radio" name="degree_type" value="MS"
                        <?php if (($_POST['degree_type']) == 'MS') {echo 'checked="checked"';} ?>
                      >MS
                      <input type="radio" name="degree_type" value="MBA"
                        <?php if (($_POST['degree_type']) == 'MBA') {echo 'checked="checked"';} ?>
                      >MBA
                      <input type="radio" name="degree_type" value="JD"
                        <?php if (($_POST['degree_type']) == 'JD') {echo 'checked="checked"';} ?>
                      >JD
              </td>
            </tr>
           ETC....

I get an "undefined index" error on each of the HTML lines referencing a radio button. I understand this might be easier to do in JavaScript, but I don't know much about JS... A detailed response would be much appreciated!

Thanks!

  • 写回答

5条回答 默认 最新

  • dreamone5156 2013-07-31 15:30
    关注

    If you're getting an undefined error on the HTML page, just can add an isset() check to the logic where you're printing out the value. E.g.:

    <input type="radio" name="degree_type" value="JD" <?php if (($_POST['degree_type']) == 'JD') {echo 'checked="checked"';} ?> >JD
    

    Becomes

    <input type="radio" name="degree_type" value="JD" <?php if (isset($_POST['degree_type']) && $_POST['degree_type'] == 'JD') {echo 'checked="checked"';} ?>>JD
    
    评论

报告相同问题?

悬赏问题

  • ¥15 r语言神经网络自变量重要性分析
  • ¥15 基于双目测规则物体尺寸
  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢