简体   繁体   中英

php contact form submit with jquery validation

I have a basic enough contact form that uses jquery client side validation. I started by doing the form in ajax, but now think it will be better to submit it server side, as I need a success page to add Google Analytics tracking on. So the ajax form went like this:

    if (i == 0) {
    $.ajax({        
        beforeSend: preloader,
        type    : 'POST',
        url     : './ajax/contact-form.php',
        data    : $('#contact_form').serialize(),
        success : function(data) {
            $('#popup_form').html(data);
        }
    });
}

But now I want to change it to

if (i == 0) // then go to success.php, else do not submit the form. 

The i==0 is obviously the validation error count. So how can I disable the form from submitting if i doesn't = 0?

If it helps, here is my form

<form action="success.php" id="contact_form" method="post">
<input id="contact_name" name="contact_name" placeholder="name*" type="text" /><span id="contact_name_validate"></span>
<input id="contact_company" name="contact_company" placeholder="company" type="text" />
<input id="contact_email" name="contact_email" placeholder="email*" type="text" /><span id="contact_email_validate"></span>
<input id="contact_telephone" name="contact_telephone" placeholder="telephone*" type="text" /><span id="contact_telephone_validate"></span>
<div id="contact_message"><textarea name="contact_message" placeholder="Enter your message..."></textarea></div>
<input id="contact_button" type="submit" value="Enquire Now" />

Is what you are looking for return false?

if (i == 0) {
    return true; // submits the form if this is a button click / form submit event handler
}

return false; // notice this, stops form from submitting.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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