By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,966 Members | 1,949 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,966 IT Pros & Developers. It's quick & easy.

Return to a form from PHP using Ajax?

P: 12
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 onreadystatechange. Here is the call from the form:

Expand|Select|Wrap|Line Numbers
  1. <form id="frmMeetReg" action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="POST" onsubmit="return DBinsert();"> 
  2.  
  3. Here is my code in javascript:
  4.  
  5. var canformsubmit=false;
  6.  
  7. function DBinsert(){
  8. var divid = 'output2'; 
  9. var loadingmessage = 'Processing...'; 
  10. var emMsg = "";
  11. var fnMsg = "";
  12. var lnMsg = "";
  13. var cbMsg = "";
  14. var srvRsp = "";
  15. var xmlHttp;
  16.  
  17. if(canformsubmit==false){
  18. //code to call ajax
  19.   xmlHttp=GetXmlHttpObject();
  20.   if (xmlHttp==null){
  21.   alert ("Browser does not support HTTP Request");
  22.   return;
  23.   }
  24.   xmlHttp.onreadystatechange = function(){ 
  25.   if(xmlHttp.readyState > 0 && xmlHttp.readyState < 4){ 
  26.     document.getElementById(divid).innerHTML=loadingmessage; 
  27.     } 
  28.   if (xmlHttp.readyState == 4) { 
  29.     srvRsp = xmlHttp.responseText; 
  30.     document.getElementById(divid).innerHTML=xmlHttp.responseText; 
  31.     if (srvRsp == 'FAILURE') {
  32.       document.getElementById(divid).innerHTML="ERROR ON SERVER, PLEASE TRY AGAIN!";
  33.       canformsubmit=false;
  34.       alert("ERROR ON SERVER, PLEASE TRY AGAIN!");
  35.       }
  36.     if (srvRsp == 'SUCCESS') {
  37.       document.getElementById(divid).innerHTML="SUCCESSFUL UPDATE TO DATABASE, ON TO PayPal!";
  38.       canformsubmit=true;
  39.       return canformsubmit;
  40.       }
  41.     } 
  42.   else {
  43.     canformsubmit=false;
  44.     }
  45.   } 
  46.  
  47.   if ((document.getElementById('custom').value == "")) {
  48.     emMsg = "MUST ENTER EMAIL!";
  49.   }
  50.   if (emBoolean == 'true') {
  51.     emMsg = "MUST ENTER AN UNREGISTERED EMAIL!";
  52.   }
  53.   if (document.getElementById('first_name').value == "") {
  54.     fnMsg = "MUST ENTER FIRST NAME!";
  55.   }
  56.   if (document.getElementById('last_name').value == "") {
  57.     lnMsg = "MUST ENTER LAST NAME!";
  58.   }
  59.   if (CalcButtonPressed == false) {
  60.     cbMsg = "MUST CLICK THE CALCULATE BUTTON!";
  61.   }
  62.   if ((CalcButtonPressed == true) && (!(document.getElementById('custom').value== "")) 
  63.     && (!( document.getElementById('first_name').value == "")) 
  64.     && (!(document.getElementById('last_name').value == "")) 
  65.     && (emBoolean == 'false') && (!(srvRsp == 'FAILURE'))) {
  66.     var queryString = '&email=' + (encodeURIComponent(document.getElementById('custom').value)) + 
  67.     '&first_name=' + (encodeURIComponent(document.getElementById('first_name').value)) + .................
  68.  
  69.     xmlHttp.open("POST", "omrDBinsertTest.php", true);
  70.     xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 
  71.     xmlHttp.setRequestHeader("Content-length", queryString.length); 
  72.     xmlHttp.setRequestHeader("Connection", "close"); 
  73.     xmlHttp.send(queryString); 
  74.     canformsubmit=true;
  75.     }
  76.     else {
  77.       var queryString = '&em=' + emMsg + '&fn=' + fnMsg + '&ln=' + lnMsg + '&cb=' + cbMsg + '&sr='      + srvRsp;
  78.  
  79.       xmlHttp.open("POST", "omrReady.php", true);
  80.       xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 
  81.       xmlHttp.setRequestHeader("Content-length", queryString.length); 
  82.       xmlHttp.setRequestHeader("Connection", "close"); 
  83.       xmlHttp.send(queryString); 
  84.       canformsubmit=true;
  85.       }
  86.   }
  87. return canformsubmit;
  88. }
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=true;?

Thanks!
May 10 '10 #1
Share this Question
Share on Google+
5 Replies


dlite922
Expert 100+
P: 1,584
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
May 11 '10 #2

P: 12
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.responseText 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.
May 12 '10 #3

dlite922
Expert 100+
P: 1,584
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
May 12 '10 #4

P: 12
@dlite922
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("POST", "xxxxxxxxt.php", true); statement other than the xmlHttp.responseText?
May 14 '10 #5

dlite922
Expert 100+
P: 1,584
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
May 14 '10 #6

Post your reply

Sign in to post your reply or Sign up for a free account.