Javascript returning values confusion

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
Eric Praline
Forum Commoner
Posts: 32
Joined: Wed Aug 29, 2007 8:37 am

Javascript returning values confusion

Post 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.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Javascript returning values confusion

Post 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.
Eric Praline
Forum Commoner
Posts: 32
Joined: Wed Aug 29, 2007 8:37 am

Re: Javascript returning values confusion

Post 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
 
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Re: Javascript returning values confusion

Post by Kieran Huggins »

Dude, you sooooo need to ditch all those sit-ups for jQuery.
Eric Praline
Forum Commoner
Posts: 32
Joined: Wed Aug 29, 2007 8:37 am

Re: Javascript returning values confusion

Post 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!
User avatar
arjan.top
Forum Contributor
Posts: 305
Joined: Sun Oct 14, 2007 4:36 am
Location: Hoče, Slovenia

Re: Javascript returning values confusion

Post 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
}
 
 
Post Reply