1

I have a problem with a wordpress widget that I developed.

It 's just a form with an email field, a button and a checkbox.

When the user click on submit, I want to verify if the checkbox is ticked and IF YES submit the form...

My problem is that the form is submitted even tough the checkbox is not ticked. The page is reloaded.

Here is a short version of my code :

<?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>

1 Answer 1

6

Add a simple onsubmit handler to the form, e.g.:

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

Demo: http://jsfiddle.net/vqVEF/

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks dude! I used jQuery to validate my form and I just added "return this.rules.checked;" and it does the trick. Wish you a nice day.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.