PHP仅在勾选复选框时提交表单

Jk_*_*Jk_ 1 php validation wordpress

我开发的wordpress小部件有问题.

它只是一个带有电子邮件字段,按钮和复选框的表单.

当用户点击提交时,我想验证是否勾选了复选框,如果是,则提交表单...

我的问题是表单提交甚至很难复选框没有勾选.页面重新加载.

这是我的代码的简短版本:

<?php if(isset($_POST['rules']) && $_POST['rules'] == 'Yes') 
{
        echo 'The checkbox was selected'; 
} else {
        echo 'The checkbox wasn\'t selected';
} ?>

<form id="form-invite" method="post" action="">
    <label>Your friend(s) email :</label>
    <br/>
    <small style="text-transform:uppercase;font-family:Arial;font-size:9px;color:#333;"<em>Multiple emails separated by comma.</em></small>
    <input class="kolom" type="text" name="email" id="email"/>
    <input class="button-primary classic-button" type="submit" name="send-them" value="Send Invitation"/>
    <input type="checkbox" name="rules" value="Yes" /> <span id="rulesInfo">I read the Privacy Policy</span><br/>
    <span id="emailInfo"></span>
</form>
Run Code Online (Sandbox Code Playgroud)

kar*_*m79 6

onsubmit向表单添加一个简单的处理程序,例如:

<script>
    document.getElementById("form-invite").onsubmit = function() {
        return this.rules.checked;
    }
</script>
Run Code Online (Sandbox Code Playgroud)

演示:http://jsfiddle.net/vqVEF/