views:

73

answers:

3

Hello I have two dependants select box, the second one is popularited after onchange event.

The first one is loaded with a selected value (selected=selected), what I'm asking, it is possible to send the requested while the page is loading, ie as I have the the selected option, just send the request.

Javascript

function getXMLHTTP() {
        var xmlhttp=false;  
        try{
            xmlhttp=new XMLHttpRequest();
        }
        catch(e)    {       
            try{            
                xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch(e){
                try{
                xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
                }
                catch(e1){
                    xmlhttp=false;
                }
            }
        }

        return xmlhttp;
    }

    function getSubCat(catId,incat) {   
        var strURL="../Includes/subcatAds.php?SubCat="+catId+"&incat="+incat;
        var req = getXMLHTTP();

        if (req) {

            req.onreadystatechange = function() {
                if (req.readyState == 4) {
                    // only if "OK"
                    if (req.status == 200) {                        
                        document.getElementById('subcat').innerHTML=req.responseText;                       
                    } else {
                        alert("There was a problem while using XMLHTTP:\n" + req.statusText);
                    }
                }               
            }           
            req.open("GET", strURL, true);
            req.send(null);
        }       
    }

The PHP will be provided if needed

+1  A: 

You could use the onload event like this:

window.onload = function(){
  var selectbox = document.getElementById('select box id');

  if (selectbox.value !== ''){
    // your code to send ajax requests...
  }
};

The code runs as soon as page loads. It then checks if the value of the specified select box is not empty meaning something is selected; in that case you put your code for the ajax request which will execute.

Sarfraz
Thanks for ur help, it works great
jartaud
instead of window.onload, which waits for javascript/images, it's better to use dom ready events. see: http://www.thefutureoftheweb.com/blog/adddomloadevent
vol7ron
+3  A: 

You really need to use jQuery and replace all of the above with:

function getSubCat(catId, incat)
  { 
    $('#subcat').load("../Includes/subcatAds.php?SubCat="+catId+"&incat="+incat);
  }


// Run on load:
$( function(){ 
               getSubCat(4, 5);
   });

It will do the same thing. It's set up to run on load, and you don't have to worry about cross browser compatibility.

You will find yourself using jQuery selectors all the time and your code will be very lightweight. If you link the jQuery library to Google servers people will not even have to download it. Most people have it in cache already.

Sebastián Grignoli
Thanks for your answer, but what if I want to use getSubCat with the onchange??
jartaud
jQuery is crap. It's coded for people who don't know how to code, by people that don't know how to code. It's far too often that anything I've tried jQuery with has needed amending. For something not light-weight, it's not worth your time.
vol7ron
@vol7ron surely they gonna give you a real preach. :)
jartaud
@vol7ron Stack Overflow is not the place for a rant.
Sebastián Grignoli
@jartaud jQuery lets you add listener functions to common events on DOM elements very easily: $('#idOfSomeInputElement').change(function(event){ getSubCat($(this).val(), $('#idOfOtherInput').val());})
Sebastián Grignoli
This will call the anonymous function when the value of idOfSomeInputElement changes. The anonymous function will call your function, passing the values of this input element and another one that you might have on your page.
Sebastián Grignoli
You can also specify a callback for when the Ajax request fails, as you did in your example.Check http://www.visualjquery.com for a fast jQuery API reference. Believe me, you will love it.
Sebastián Grignoli
@Sebastián Grignoli thank you, man it realy works, the site is prety cool too.
jartaud
Thanks again che!, so instead of this if (req.status == 200){ document.getElementById('subcat').innerHTML=req.responseText; } else {alert("There was a problem while using XMLHTTP:\n" + req.statusText);}, I can just use ajaxError(callback) ??
jartaud
Yes, but you have to use the .ajax() method instead of the .load() shortcut method I used, wich is oversimplified.Take a look at this jQuery Ajax tutorial:http://articles.sitepoint.com/article/ajax-jquery
Sebastián Grignoli
Or look at the $.ajax() method on http://www.visualjquery.com .It's really simple and cover all scenarios.
Sebastián Grignoli
@Sebastián Grignoli: I don't count my comment as a rant. `you really need to use jQuery` is rant. ... you, my friend, really need to learn how to program and not depend on jQuery.
vol7ron
I think I know how to program, but I'd like to understand your point, so what book (or article or anything) do you think that I should read? Tell me and I'll read it.
Sebastián Grignoli
vol7ron, a neckbeard of such awesomeness that he forbears any libraries and codes straight up 1s and 0s.
Will
+1  A: 

Since you are doing this before getting any input from the user, you could immediately make the call to the server, before the DOM is finished, before the page is fully loaded, and then just wait until the onload event takes place, or you get informed that the DOM tree is finished, and you can then safely change the value of any of the html elements.

This way you don't have the user wait for the browser to finish loading before you even start your request, which will improve the user experience.

James Black
Thanks for your answer, u mean by aplying the Sarfraz snippet??
jartaud
@jartaud - I am not familiar with the name "Sarfraz snippet", so I can't answer that part. My thought is to do what you can to speed up the initialization.
James Black
@James Black Sarfraz is just a SOV user who've answered and have put a piece of code.
jartaud
@jartaud - You would use your code to fetch the information, and just have some variable set when the onload event happens, so you can look at that, and when it is set (true/false) then you can set the selectbox value.
James Black