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:
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!
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;
}
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!
Comment