473,395 Members | 1,440 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 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
5 2376
dlite922
1,584 Expert 1GB
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
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
1,584 Expert 1GB
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
@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
1,584 Expert 1GB
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

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

Similar topics

5
by: Martin | last post by:
Hello NG, I've been doing some AJAX for a few weeks now. The basics worked fine so far, but now I've got the following problem which I can't solve: With AJAX you typically update/replace only...
3
by: mudgilgaurav | last post by:
I am very keen to learn AJAX and want to use it in my website Can anyone give me a bunch of code abt ajax and as well as tell me the flow of code
5
by: lucyh3h | last post by:
Hi, I am trying to use XMLHttpRequest to do server side validation. I have several fields on a form and a submit button. The submit button has an event assocated with it when clicked. The...
1
by: prashanth023 | last post by:
Hi, This is prashanth kumar. I am getting error , i am gettign number of records using ajax. Please solve this if n >0 i am putting return false in response function. but form is going to...
2
by: awi | last post by:
Hi Everyone, I'm facing a unique problem when testing with FireFox (at least, I think its unique). Here is the situation: I have a page setup that looks like this: <form id="abcForm"> <div...
6
by: dmorand | last post by:
I'm having a little trouble with my ajax. I can see my results in IE, but not firefox. I'm assuming I'm missing some syntax somewhere. alert("Test " + results + testing); returns the values in...
1
by: biggie3384 | last post by:
Is it possible to submit a form using ajax? I have a form with sever inputs and when submited, it writes the input values in a jsp file. I would like to submit the form using ajax in the...
5
by: mukeshrasm | last post by:
Hi I am displaying a page (result.php) using ajax and in this page I have few links when clicked opens a div popup. in which a form, a text field and a submit button is there. when I click submit...
5
by: thatcollegeguy | last post by:
Below are my 3php and 2js files. I create a table using ajax/php and then want to change the values in the tables add(+ number for teamid) id's for each specific td in the table. I don't know...
1
by: mfaisalwarraich | last post by:
I am trying to get values posted by a form using ajax but i dont know how to get them. im opening a form which is also based on the ajax. here is the code. function showPage(page) { var...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.