Return to a form from PHP using Ajax?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • HawkMan24
    New Member
    • Apr 2010
    • 12

    Return to a form from PHP using Ajax?

    I submitted this on another PHP/Ajax forum and no one was able to figure it out. I am trying to return to a form from Ajax after clicking on a PayNow button and I want to return true or false depending on if it was able to write a record to my database from a php script. Everything works fine if the record gets written (it continues to PayPal) or if somemone leaves out a required field (it displays a message and stays on the page). But my problem is if it trys to write the record and I get an error, it comes back with a true instead of a false and displays the error message as it should but continues on to PayPal, which I don't want it to do. I believe my logic problem results from my lack of understanding of the onreadystatecha nge. Here is the call from the form:

    Code:
    <form id="frmMeetReg" action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="POST" onsubmit="return DBinsert();"> 
    
    Here is my code in javascript:
    
    var canformsubmit=false;
    
    function DBinsert(){
    var divid = 'output2'; 
    var loadingmessage = 'Processing...'; 
    var emMsg = "";
    var fnMsg = "";
    var lnMsg = "";
    var cbMsg = "";
    var srvRsp = "";
    var xmlHttp;
    
    if(canformsubmit==false){
    //code to call ajax
      xmlHttp=GetXmlHttpObject();
      if (xmlHttp==null){
      alert ("Browser does not support HTTP Request");
      return;
      }
      xmlHttp.onreadystatechange = function(){ 
      if(xmlHttp.readyState > 0 && xmlHttp.readyState < 4){ 
        document.getElementById(divid).innerHTML=loadingmessage; 
        } 
      if (xmlHttp.readyState == 4) { 
        srvRsp = xmlHttp.responseText; 
        document.getElementById(divid).innerHTML=xmlHttp.responseText; 
        if (srvRsp == 'FAILURE') {
          document.getElementById(divid).innerHTML="ERROR ON SERVER, PLEASE TRY AGAIN!";
          canformsubmit=false;
          alert("ERROR ON SERVER, PLEASE TRY AGAIN!");
          }
        if (srvRsp == 'SUCCESS') {
          document.getElementById(divid).innerHTML="SUCCESSFUL UPDATE TO DATABASE, ON TO PayPal!";
          canformsubmit=true;
          return canformsubmit;
          }
        } 
      else {
        canformsubmit=false;
        }
      } 
      
      if ((document.getElementById('custom').value == "")) {
        emMsg = "MUST ENTER EMAIL!";
      }
      if (emBoolean == 'true') {
        emMsg = "MUST ENTER AN UNREGISTERED EMAIL!";
      }
      if (document.getElementById('first_name').value == "") {
        fnMsg = "MUST ENTER FIRST NAME!";
      }
      if (document.getElementById('last_name').value == "") {
        lnMsg = "MUST ENTER LAST NAME!";
      }
      if (CalcButtonPressed == false) {
    	cbMsg = "MUST CLICK THE CALCULATE BUTTON!";
      }
      if ((CalcButtonPressed == true) && (!(document.getElementById('custom').value== "")) 
        && (!( document.getElementById('first_name').value == "")) 
        && (!(document.getElementById('last_name').value == "")) 
        && (emBoolean == 'false') && (!(srvRsp == 'FAILURE'))) {
        var queryString = '&email=' + (encodeURIComponent(document.getElementById('custom').value)) + 
        '&first_name=' + (encodeURIComponent(document.getElementById('first_name').value)) + .................
     
        xmlHttp.open("POST", "omrDBinsertTest.php", true);
        xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 
        xmlHttp.setRequestHeader("Content-length", queryString.length); 
        xmlHttp.setRequestHeader("Connection", "close"); 
        xmlHttp.send(queryString); 
    	canformsubmit=true;
    	}
        else {
          var queryString = '&em=' + emMsg + '&fn=' + fnMsg + '&ln=' + lnMsg + '&cb=' + cbMsg + '&sr='      + srvRsp;
    
          xmlHttp.open("POST", "omrReady.php", true);
          xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 
          xmlHttp.setRequestHeader("Content-length", queryString.length); 
          xmlHttp.setRequestHeader("Connection", "close"); 
          xmlHttp.send(queryString); 
          canformsubmit=true;
          }
      }
    return canformsubmit;
    }
    omrDBinsertTest .php is just a test script that doesn't actually write to the database, it just has the statement in it die('FAILURE'); so I can test this problem.

    omrReady.php just echo's back the messages for if the names or email aren't filled in or if the Calculate button wasn't pressed or if there was a server error.

    I even threw in the if statement at the end to try and set the canformsubmit to false.

    How is it going to the omrDBinsertTest .php script, echoing back FAILURE and leaving canformsubmit=t rue;?

    Thanks!
  • dlite922
    Recognized Expert Top Contributor
    • Dec 2007
    • 1586

    #2
    Just trace your code and use alert() to output a variable's value at each point to see what your program is doing.

    Might get better help in the JavaScript forum.

    Also look into jquery, you might be able to reduce your entire script to a few lines.



    Dan

    Comment

    • HawkMan24
      New Member
      • Apr 2010
      • 12

      #3
      Ok, I put alerts throughout the code, what happens is it goes to my script, comes out of it with the word failure, I check what the xmlHttp.respons eText is and if falilure, set the form submit variable to false or true if it is Success and then it drops out of the if statement and returns true. The problem is I am setting the variable during the readystate = 4 and this can happen anytime, so it can drop out of the if statement before it gets the response back. How can I keep it from performing the return statement until the readystate is equal to 4.

      Comment

      • dlite922
        Recognized Expert Top Contributor
        • Dec 2007
        • 1586

        #4
        You have 3 or 4 return statement, I'm not sure which you're referring to.

        No matter which one it is, can't you wrap it in an IF statement. e.g. If readystate = 4, then return?



        Dan

        Comment

        • HawkMan24
          New Member
          • Apr 2010
          • 12

          #5
          Originally posted by dlite922
          You have 3 or 4 return statement, I'm not sure which you're referring to.

          No matter which one it is, can't you wrap it in an IF statement. e.g. If readystate = 4, then return?



          Dan
          The 3 or 4 return statements are there for testing. An if statement won't work because when the if statement is false, it continues after the if statement and drops out of the function and goes back to the form with whatever the onsubmit has been set to. I tried a do while onreadystate not equal to 4 to try and have it sit there until it gets the response back, but that would time out sometimes, so not really a good option. Any other ideas! Is there any other way to return a value from a xmlHttp.open("P OST", "xxxxxxxxt.php" , true); statement other than the xmlHttp.respons eText?

          Comment

          • dlite922
            Recognized Expert Top Contributor
            • Dec 2007
            • 1586

            #6
            I don't think there. If you think you're script won't return or time out, you can have your own timer and time out within 5 or 10 seconds and show failed, try again - message.






            Dan

            Comment

            Working...