-1

Please help me, How to use two ajax field validation on button (if data already exist don't click on button or redirect same page) redirection stop?

This is my ajax code:

function validate()
{
 return (chkunm() && chkemail());

}    

function chkunm()
{
   // alert ("hello");
   var unm, http;
   unm = document.getElementById("pupilname").value;
   http= new XMLHttpRequest();
   http.onreadystatechange=function(){
       if (http.readyState==4 && http.status==200){
           document.getElementById("s1").innerHTML=http.responseText;
       }
       if(http.responseText==true)
       {
         //  alert ("Welcome");
           return true;
       }
       else
       {
          // alert ("username already exists");
           return false;
       }
   }
   http.open("GET","getunm.php?unm="+unm,true);
   http.send();
}
function chkemail()
{
// alert("hello");
// die();
   var email, http;
   email = document.getElementById("pupilpass").value;
   http= new XMLHttpRequest();
   http.onreadystatechange=function(){
       if (http.readyState==4 && http.status==200){
           document.getElementById("s2").innerHTML=http.responseText;
       if(http.responseText==true)
        {
         //  alert ("welcome");
           return true;
       }
       else
       {
           //alert ("email already exists");
           return false;
       }
   }
   }
   http.open("GET","getemail.php?email="+email,true);
   http.send();
}

I want, if data is already exist...!! SO don't click on button .. How can possible.. Help me

Thanks

  • 1
    what have you done so far ? – Vivek Singh May 30 '16 at 13:39
  • Please refer [how to ask](http://stackoverflow.com/help/mcve). – Shrabanee May 30 '16 at 13:45
  • Questions seeking help must include shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). – Gustavo Morales May 30 '16 at 13:46
  • Welcome to SO. Please read [What topics can I ask about](http://stackoverflow.com/help/on-topic) and [How to ask a good question](http://stackoverflow.com/help/how-to-ask) And [the perfect question](http://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/) SO is **not a free coding or code conversion or tutorial or library finding service** You also have to show that you have made some effort to solve your own problem. – RiggsFolly May 30 '16 at 13:47

1 Answers1

0

This answer might help you:

Basically you will have multiple ajax calls that bring back their check results. You will use when to wait for both before checking if you want to redirect or not.

Code:

$(document).ready(function(){
   var check1, check2;
   $("yourElement(s)").click(function(){
      $.when(ajax1(), ajax2()) {
         if(check1 || check2) {
            // window.location - whatever your soul desires
         }
      }
   }
   function ajax1() {
     return $.ajax({
        url: "someUrl",
        dataType: "json",
        data:  yourJsonData,            
        // whatever else you need,
        success: function(result){  
           check1 = result;
        }
     });
   }
   // idem ajax 2 to get check2 value.
}

As you might have guessed, this code is not quite tested.

I hope this helps.

Community
  • 1
  • 1
Petre Ionescu
  • 174
  • 1
  • 8
  • Hi Peter, I have create two field username and email (ajax call).. Ok then both field are new data ... button is redirect sign up form otherwise no redirect or same page redirection. How can possible .. ? how to create condition ? Thanks – Rajendra vairagi May 30 '16 at 16:54
  • I have edited my response. Maybe it is more helpful to you now. – Petre Ionescu May 31 '16 at 08:02