PDA

View Full Version : two forms javascript checkbox conflict



ianhaney
07-16-2019, 06:34 PM
I have two contact forms, one is in includes/footer.php and one on contact.php and when on the contact page, if I try to submit the form in the footer, it won't submit because I got checkbox validation on each form that the user has to agree to the terms before the form submits so tried the following but either form still don't submit unless both checkboxes are checked

the form checkbox code in the footer.php is below



<div class="col-md-12 mb-3">
<input type="checkbox" id="agreefooter" /> I have read and agree to MM PC Solutions <a href="privacy-policy.php" target="_blank">Privacy Policy</a>
</div>

<script>
$(".needs-validation").submit(function(e){
if(!$("#agreefooter").is(":checked")){
alert('Please indicate that you have read MM PC Solutions Privacy Policy');
e.preventDefault();
}
});
</script>


The form checkbox code on the contact page is below



<div class="col-md-12 mb-3">
<input type="checkbox" id="agreecontact" /> I have read and agree to MM PC Solutions <a href="privacy-policy.php" target="_blank">Privacy Policy</a>
</div>

<script>
$(".needs-validation contactform").submit(function(e){
if(!$("#agreecontact").is(":checked")){
alert('Please indicate that you have read MM PC Solutions Privacy Policy');
e.preventDefault();
}
});
</script>


Can anyone help please so the forms don't conflict and allows to me submit the form if either form does not have the checkbox in their form checked

Hope that makes sense

ianhaney
07-16-2019, 07:14 PM
I have managed to solve the issue now with similar coding but put onsubmit in the form tag itself and changed the id in the onsubmit code and the checkbox id code