Javascript / AJAX query

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 / AJAX query

Post by Eric Praline »

~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.


Hello peeps

I'm developing an automated library book-lending system, which works fine, but I've got one little thing I want to add that I can't get to work.
When a someone turns up with the book they want to borrow, I have a loan slip form which has borrower details already entered (name, date borrowed, due date of return etc)... the only field that needs completing is the individual copy number of the book (every book in the library has a unique copy no). Most books in the library are loan copies, but some are reference only.

When the form is submitted a javascript function is called (... onSubmit="return validateForm()") which calls 2 functions that perform validation:
1) (checkBlankCopyNo) checks whether the copy no field is empty, and if so displays a javascript alert and stops the form being submitted
2) (ajaxCheckRef) if the field has a number entered, this checks if the copy number entered is of a reference or loan copy - if reference it should display an alert and stop the form submitting, else it would process the form

Now, this is where the problem lies... if I call function no 2 directly from the form (from a button for example) the AJAX function works as intended; but if it gets called from the Submit button, whereby the blank field check is performed, then the copy type check, no data seems to be sent back from the AJAX function.

It sounds really complicated when I've written it down, but in practice it isn't too bad! The code is below:

Code: Select all

 
/* VALIDATE FORM FUNCTIONS */
function validateForm() {
    var checkValue;
    checkValue = checkBlankCopyNo();
    if (checkValue == false) { return checkValue; }
    return ajaxCheckRef();
}
/* END */
 
/* FUNCTION TO CHECK IF COPY NO HAS BEEN LEFT BLANK */
function checkBlankCopyNo() {
    if (document.forms["NewLoan"]["CopyNo"].value == "") {
        alert("You must enter an item Copy Number!");
        document.forms["NewLoan"]["CopyNo"].focus();
        return false;
    }
    return true;
}
 
/* FUNCTION TO CHECK IF ENTERED COPY NO IS A REFERENCE COPY */
function ajaxCheckRef() {
    var copyNo = document.forms["NewLoan"]["CopyNo"].value;
    http.open("GET","ValidateCopy.php?CopyNo="+copyNo,true);
    http.onreadystatechange = handleRefResponse;
    http.send(null);
}
/* END */
 
function handleRefResponse() {
    if (http.readyState==4) {
        var loanType=http.responseText;
        /* do stuff here */
    }
}
/* END */
 
I'm slightly confuzzled as to why calling the AJAX function directly works (the variable loanType contains the correct info sent back from the php file), but when it's the second 'sub-function' no data is returned (loanType is empty), even though the AJAX functions are performed (checked with a simple alert in the handleRefResponse() function).

Anyone of you brainiacs got any ideas? ... cos I'm stumped! :?


~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.
Eric Praline
Forum Commoner
Posts: 32
Joined: Wed Aug 29, 2007 8:37 am

Re: Javascript / AJAX query

Post by Eric Praline »

So, no-one got any ideas? I'm completely stumped at the minute!

The short version of my problem is thus:
HTML Form A calls Javascript Function X on submit.
Javascript Function X (ValidateForm) calls Function Y to check a field in Form A is completed, and if so...
Javascript Function X then calls Function Z, which is the AJAX function to check if the record (the unique ID of which is entered in the field from Form A) is a Loan or Reference copy.

For some reason, no data gets returned when doing this as above... however if HTML Form A calls Javascript Function Z directly, then all is fine, and I get the correctly returned value!

So, either I'm doing something wrong (most likely!); possibly with the returning of values, or maybe it's just my bad coding :lol:
Otherwise it's a bug either with Javascript and/or AJAX, which doesn't seem likely to me... :wink:

Somebody help me please!
Post Reply