Page 1 of 1

Javascript returning values confusion

Posted: Wed May 28, 2008 6:01 am
by Eric Praline
Hi guys

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 -->
 
Just a simple HTML form which onSubmit performs the checkType() function...

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;
    }
}
 
The PHP page gets the CopyNo entered from the form, and checks the database to see whether the item is a Loan or Reference copy, and returns (via responseText) either 'Loan' or 'Reference'.

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.

Re: Javascript returning values confusion

Posted: Wed May 28, 2008 2:31 pm
by califdon
Are you omitting the rest of the Ajax call?? What you have shown is not a valid Ajax call, to the best of my knowledge. You have to create an instance of the xmlHttpRequest object, test for what browser is sending the request, etc. etc. If you're not doing all that, I don't think it's going to work.

Re: Javascript returning values confusion

Posted: Wed Jun 04, 2008 4:03 am
by Eric Praline
Yes, I'm doing a full AJAX call, but didn't show it since everyone knows what it looks like by now!
Anyway, for clarity's sake, here is the Javascript code I use...

Code: Select all

 
var http = createRequestObject();
 
function createRequestObject() {
    var reqObj;
    try {
    // Firefox, Opera 8.0+, Safari
        reqObj=new XMLHttpRequest();
    }
    catch (e) {
    // Internet Explorer
        try {
            // IE 6.0+
            reqObj=new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e) {
            try {
                // IE 5.5+
                reqObj=new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e) {
                // old browser
                alert("Your browser does not support AJAX!");
                return false;
            }
        }
    }
    return reqObj;
}
 
And the PHP file ValidateCopy.php is also included below...

Code: Select all

 
include 'dbInfotreeConnect.php';
 
$CopyNo = $_GET['CopyNo'];
$type="unknown";
 
// SEARCH TO FIND IF SELECTED COPY IS REFERENCE ONLY
$queryCopySQL = "SELECT LoanType FROM copyRecord WHERE CopyID = $CopyNo";
$queryCopy = mysql_query($queryCopySQL);
while ($resultRow = mysql_fetch_array($queryCopy)) {
    $LoanType = $resultRow['LoanType'];
    if ($LoanType=='Reference') {
        $type="Reference";
    } else {
        $type="Loan";
    }
}
echo $type;
// END
 

Re: Javascript returning values confusion

Posted: Wed Jun 04, 2008 7:32 am
by Kieran Huggins
Dude, you sooooo need to ditch all those sit-ups for jQuery.

Re: Javascript returning values confusion

Posted: Fri Jun 06, 2008 5:31 am
by Eric Praline
Well I've only just got into using AJAX, and I've never particularly liked Javascript... so I don't fancy trying to learn another language - although from the quick look I had it does seem to be quite useful, so something to take a look at in the future.

However, what I'm trying to do seems (to me) to be fairly simple, although it could be more complicated than it looks!

Re: Javascript returning values confusion

Posted: Sat Jun 07, 2008 11:52 am
by arjan.top

Code: Select all

 
function checkType() {
    var copyNo = document.forms["NewLoan"]["CopyNo"].value;
   
    http.open("GET","ValidateCopy.php?CopyNo="+copyNo,false);
   
    http.onreadystatechange = handleResponse;
    http.send(null);
    //do something with http.responseText
}