【问题标题】:Validation of multiple checkbox and implode function验证多个复选框和内爆函数
【发布时间】:2014-04-12 07:59:17
【问题描述】:

下面是sn-p

<tr>
    <td>7</td>
    <td>Tick the Teaching Methods Used </td>
    <td>
        <input type="checkbox" id="lectures" name="lectures" value="lectures">Lectures &nbsp;
        <input type="checkbox" id="study" name="study" value="study">Case Study &nbsp;
        <input type="checkbox" id="audvid" name="audvid" value="audvid">Audio|Video &nbsp;
        <input type="checkbox" id="interactive" name="interactive" value="interactive">Interactive Methods &nbsp;
        <input type="checkbox" id="discussion" name="discussion" value="discussion">Discussion &nbsp;
        <input type="checkbox" id="role" name="role" value="role">Role Play &nbsp;
        <input type="checkbox" id="quiz" name="quiz" value="quiz">Quiz
    </td>
</tr>

验证码是

if ((document.form1.lectures.checked == false)
      && (document.form1.study.checked == false)
      && (document.form1.audvid.checked == false)
      && (document.form1.interactive.checked == false)
      && (document.form1.discussion.checked == false)
      && (document.form1.role.checked == false)
      && (document.form1.quiz.checked == false)) { 

    alert("Please check any one method"); 
    isValid = false;
}
return isValid;

我如何只将检查的值插入 mysql 数据库,implode 似乎没有帮助

编辑:如果我对所有复选框内爆使用相同的“名称”但在这种情况下我无法验证

【问题讨论】:

标签: javascript php mysql forms validation


【解决方案1】:

为所有复选框使用相同的名称。你说implode,那很好。至于验证,将其更改为使用 ID:

else if ((document.getElementById("lectures").checked == false)
      && (document.getElementById("study").checked == false)
      && (document.getElementById("audvid").checked == false)
      && (document.getElementById("interactive").checked == false)
      && (document.getElementById("discussion").checked == false)
      && (document.getElementById("role").checked == false)
      && (document.getElementById("quiz").checked == false)) { 

    alert("Please check any one method"); 
    isValid = false;
}
return isValid;

【讨论】:

    【解决方案2】:

    如果选中(选中)复选框,则仅在 POST 中放置。

    PHP:

    $keys = explode(',','lectures,study,audvid'); //get all keys
    $result = array();
    
    foreach ($keys as $key) {
      if (isset($_POST[$key]) && $_POST[$key]) { //selected!
        $result[] = $key;
      }
    }
    
    print_r($result); //only selected checkeboxes are in result.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-29
      • 1970-01-01
      • 2012-01-27
      相关资源
      最近更新 更多