简体   繁体   English

单击按钮时执行查询(如果选中了复选框)

[英]Execute query on button click If checkbox is checked

I want to execute a script if checkbox is checked when I click on the submit button. 如果我单击提交按钮时选中了复选框,我想执行脚本。

I got the following code snippet in my overview.php : 我的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>

And this is a code snippet from my update.php : 这是我的update.php的代码片段:

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

But it does not work for me. 但这对我不起作用。

Solution: Consider the following JSFiddle 解决方案:考虑以下JSFiddle

Explanation: This will validate to see that at least checkbox 'box[4]' has been ticked. 说明:这将验证是否已至少选中复选框“ box [4]”。

The "key" code here being the return function in the following line. 这里的“键”代码是下一行的return函数。

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

Update: Made to accommodate extra information given in comment. 更新:旨在容纳注释中提供的其他信息。

Remember: The validation here is done on the client side so that it doesn't need to query the server ( PHP ) first 请记住:此处的验证是在客户端完成的,因此不需要先查询服务器( PHP

You can do it as I have mentioned in a post on pastebin 您可以按照我在pastebin上的帖子中提到的那样进行操作

Hope it helps you dude. 希望它对您有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM