Javascript Functions not being Called in Oder?

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
millsy007
Forum Commoner
Posts: 78
Joined: Wed Jul 02, 2008 7:00 pm

Javascript Functions not being Called in Oder?

Post by millsy007 »

I have some javascript/AJAX that runs a php script to output a coach schedule into a html table. It takes the value that is in the 'date' text box and brings out the matching data.
The code works when it is called by a seperate 'get schedule' button click.
However what I want to happen is - the user select a date using a calendar, this date be entered into the textbox, and then upon the onChange of this textbox containing the date the javascript be called.

However my onChange is only working when a user manually enters a date.
Or after the user clicks the calendar, the problem with this is that it would use the date currently in the date textbox when running the getschedule() whereas I want it to use the date the user is selecting after selecting from the calendar

<input type="text" name="date" id="date" size=25 onChange="GetSchedule()">
<a href="#" onClick="cal18.select(document.forms[0].date,'calendar','MM/dd/yyyy'); GetSchedule();" name="calendar" id="calendar" style="border:none">
<img src="images/calendar.png" border="0" align="top"></a>


The GetSchedule is the last to be called, yet is still wont pass in the newly selected date to the GetSchedule code? Is there some php I could write to sort this?
User avatar
SpecialK
Forum Commoner
Posts: 96
Joined: Mon Sep 18, 2006 3:49 pm

Re: Javascript Functions not being Called in Oder?

Post by SpecialK »

When javascript sets the value of the text field, no onchange event is fired. This is why you should be manually calling that event after it is set from the calendar.

As to why GetSchedule() isn't getting the correct date, it would depend on how it is used in the function. If using AJAX, make sure it is included as part of the get/post variables. No PHP should be required to get this functionality working.
millsy007
Forum Commoner
Posts: 78
Joined: Wed Jul 02, 2008 7:00 pm

Re: Javascript Functions not being Called in Oder?

Post by millsy007 »

Thanks

GetSchedule() take the value stored in the date text box and then uses this value to run some php code:

Code: Select all

function GetSchedule(){
    var ajaxRequest;  // The variable that makes Ajax possible!
    
    try{
        // Opera 8.0+, Firefox, Safari
        ajaxRequest = new XMLHttpRequest();
    } catch (e){
        // Internet Explorer Browsers
        try{
            ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try{
                ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e){
                // Something went wrong
                alert("Your browser broke!");
                return false;
            }
        }
    }
    // Create a function that will receive data sent from the server
    ajaxRequest.onreadystatechange = function(){
        if(ajaxRequest.readyState == 4){
            var ajaxDisplay = document.getElementById('ajaxDiv');
            ajaxDisplay.innerHTML = ajaxRequest.responseText;
        }
    }
    var date = document.getElementById('date').value;
    var time = document.getElementById('time').value;
    var queryString = "?date=" + date + "&time=" + time;
    ajaxRequest.open("GET", "getschedule.php" + queryString, true);
    ajaxRequest.send(null); 
}
 
User avatar
SpecialK
Forum Commoner
Posts: 96
Joined: Mon Sep 18, 2006 3:49 pm

Re: Javascript Functions not being Called in Oder?

Post by SpecialK »

Everything looks fine to me. The only idea I currently have is that from the line

Code: Select all

onClick="cal18.select(document.forms[0].date,'calendar','MM/dd/yyyy'); GetSchedule();"
GetSchedule() might be called before the user actually clicks on the calendar to select a date. Try putting an alert between the two and see if it's being hit before the user click is.

Code: Select all

onClick="cal18.select(document.forms[0].date,'calendar','MM/dd/yyyy'); alert('date is = '+document.getElementById('date').value); GetSchedule();"
If this is getting the user selected date, then GetSchedule is doing something funky. If this isn't, and is grabbing the date in the textbox.

One solution would be to call the GetSchedule when the user selects the date. Right under the line that would put the selected date in the textbox. This way it will not be called too early and wait until the user input before it tries to submit to the server.

Code: Select all

 document.getElementById('date').value = x;
GetSchedule(); 
millsy007
Forum Commoner
Posts: 78
Joined: Wed Jul 02, 2008 7:00 pm

Re: Javascript Functions not being Called in Oder?

Post by millsy007 »

Thanks for the feedback, when I try the alert it appears before the GetSchedule() is called but the date is still not populated at this point.
I think your suggestion of adding it to the calender function makes sense, however the javascript for the calendar that I am using is huge, I spent time putting the GetSchedule at different points with no luck, any advice as to where I should put it would be much appreciated.

The source is at:
http://www.mattkruse.com/javascript/cal ... ource.html
User avatar
SpecialK
Forum Commoner
Posts: 96
Joined: Mon Sep 18, 2006 3:49 pm

Re: Javascript Functions not being Called in Oder?

Post by SpecialK »

I really like the stuff by Matt Kruse. Everything I've seen from him looks well and works nicely. Much better then the current Calendar format I have, so might be changing to this version coming up.

Try looking at the line

Code: Select all

window.CP_targetInput.value = formatDate(dt,window.CP_dateFormat);
I would think that if you put it after that line, it should grab there. As this is a pretty big library, that was my first thought just looking off the examples there.
millsy007
Forum Commoner
Posts: 78
Joined: Wed Jul 02, 2008 7:00 pm

Re: Javascript Functions not being Called in Oder?

Post by millsy007 »

Yes that does it, thanks :D
Post Reply