I'm having a bit of trouble getting an onSubmit AJAX call to work as intended... after trawling the internet I've found lots of AJAX form validation tutorials but all seem a lot more complicated and none seem to do exactly what I want...
Code: Select all
<form method="post" name="NewLoan" onSubmit="return checkType()">
<!-- form info here -->
The Javascript looks like this...
Code: Select all
function checkType() {
// get value of copyNo field
var copyNo = document.forms["NewLoan"]["CopyNo"].value;
// set URL of PHP page and send value of copyNo for checking
http.open("GET","ValidateCopy.php?CopyNo="+copyNo,true);
// call handleResponse function when receive a response from PHP script
http.onreadystatechange = handleResponse;
http.send(null);
}
function handleResponse() {
if (http.readyState==4) {
loanType=http.responseText;
}
}
If it is 'Loan', the form should submit, but if it is 'Reference' then I want to display an alert, and stop the form submitting.
Now the problem I have is getting the values to return correctly to allow or stop the form submission - in the handleResponse() function I can show an alert which correctly displays the loanType, but I can't figure out how to return a true or false value back to the original form so that it either continues submission (if loanType is 'Loan', therefore true) or stops submission (loanType is 'Reference', therefore false).
It's probably something really simple, but I can't figure it out and my head is starting to hurt! Does anyone have any ideas, or other ways to do this?
Thanks in advance.