Hello, I’m making a contact form and I have PHP validation so that the user has to have something typed in every category, email validation, etc. But I cannot get the checkbox validated.
My HTML/PHP looks like this:
<div class=“checkmark”>
<label for=“check”>Agree to Terms</label>
<input type=“checkbox” id=“check” name"check" value=“yes”/>
<?php if( isset($errors[‘check’]) ){ ?>
<label for=“check” class=“error”>You must agree to terms</label>
<?php } ?> <br />
</div>
How Can I get it so that the user has to click the checkbox in order to agree to terms, and submit the form successfully. If they don’t check the box, I was the error message to show.
The validation by PHP is done AFTER the form is sent (server side) by the script the form sends its values to. So in case of errors, you’ll have to send the form and the error messages back to the client (browser).
Now, how can that be exact same code? Pay close attention to “name” in your example and my example. Don’t just skim trough the code, read it carefully. Code highlighter should help you too.
Naturally, you get the error because your checkbox hasn’t got a NAME. You missed the equal sign when assigning the name. Hence, I even fixed it and pointed it out.
My code is working great now, but once my page initially loads, the error for my checkbox shows. I want it so that the error messages only show when the checkbox isn’t checked and the user hits the submit button.
Here’s my code…
<label for=“check”>Agree to Terms</label>
<input type=“checkbox” id=“check” name=“check” <?php echo $checked; ?>/>
<?php if(!isset($_POST[‘check’]) ){ ?>
<label for=“check” class=“error”>You must agree to terms</label>
<?php } ?>
I think, java script is best suited technique for check box validation because java script works on client machine side server load will be reduces.so best option is java script.
<script language=“JavaScript” type=“text/JavaScript”>
<!–//hide script
function checkme() {
missinginfo = “”;
if (!document.form.agree.checked) {
missinginfo += "\
You must agree to the terms";
}
if (missinginfo != “”) {
missinginfo =“__________________________________
" +
“Required information is missing:
" +
missinginfo + "
__________________________________” +
"
Please complete and resubmit.”;
alert(missinginfo);
return false;
}
else {
return true;
}
}