Ajax function in form submit
-
I’m checking my form against the user database to prevent duplicate emails before the form is submitted.
<form id="registration" enctype="multipart/form-data" method="post" action="" onsubmit="return (validate_form());">
function validate_form(){ var emailExists=''; jQuery(document).ready( function($) { data={'action': 'checkemail_exists','umail': document.getElementById('email').value}; jQuery.post( ajax_url, data, function(response) { emailExists=response; }); }); //... lots of other checks ... if(emailExists=='true'){ warning=document.getElementById('email_r'); warning.innerHTML='This email is already taken'; warning.style.display='inline'; warning.scrollIntoView(); return false; } return true; }
The problem is that the validate_form returns true every time, regardless of whether the email is already registered or not.
if I add an alert(‘wait for a moment’) before the if(emailExists==’true’) statement, then it works ok.
So clearly I need to pause the routine for long enough to allow the ajax call to complete.
What would be the approved way of doing this?
Viewing 6 replies - 1 through 6 (of 6 total)
Viewing 6 replies - 1 through 6 (of 6 total)
- The topic ‘Ajax function in form submit’ is closed to new replies.