点击按钮执行查询如果复选框被选中


Execute query on button click If checkbox is checked

如果在单击提交按钮时选中复选框,我想执行脚本。

我在overview.php:中得到了以下代码片段

<table style="width: 100%; font-size: 11;">
    <tr>
        <td colspan="6">
            <form method="post" action="update.php" target="_blank">
                <script language="JavaScript">
                function toggle(source) {
                    checkboxes = document.querySelectorAll("input[name^='box[']");
                    for(var i=0, n=checkboxes.length;i<n;i++) {
                        checkboxes[i].checked = source.checked;
                    }
                }
                </script>
                <input type="submit" name="update" value="Update - LoL / BoL">
                <input type="checkbox" onClick="toggle(this)" name="All" value="All">&nbsp;Select all
            </form>
        </td>
    </tr>
    <tr>
        <th>ve067</th>
        <th>ve378</th>
        <th>ve724</th>
        <th>ve725</th>
        <th>ve726</th>
        <th>ve727</th>
    </tr>
    <tr>
        <td>
            <form method="post" action="update.php">
                <input type="checkbox" name="box[1]" value="ve067_LB1">&nbsp;LeagueBot 1<br>
                <input type="checkbox" name="box[2]" value="ve067_LB2">&nbsp;LeagueBot 2<br>
                <input type="checkbox" name="box[3]" value="ve067_LB3">&nbsp;LeagueBot 3<br>
                <input type="checkbox" name="box[4]" value="ve067_LB4">&nbsp;LeagueBot 4<br>
            </form>
        </td>
    </tr>
</table>

这是我的update.php:中的一段代码

<?php
if(isset($_POST['update'] && $_POST['box[4]']) {
    // code here
}
?>

但它对我不起作用。

你可以像我在pastebin 上的一篇文章中提到的那样做

希望它能帮助你,伙计。

解决方案:考虑以下JSFiddle

说明:这将验证是否至少勾选了复选框"box[4]"。

这里的"键"代码是下一行中的return函数。

<input type="submit" name="update" value="Update - LoL / BoL" onclick="return validate();">

更新:用于容纳评论中提供的额外信息。

记住:这里的验证是在客户端完成的,这样它就不需要首先查询服务器(PHP