www.webdeveloper.com
Results 1 to 3 of 3

Thread: Run two XMLHttpRequests with the second based off the first

Hybrid View

  1. #1
    Join Date
    Nov 2010
    Posts
    57

    Run two XMLHttpRequests with the second based off the first

    Hey all,

    I am trying to use a javascript function to run an sql query via an external php page and then use the returned value of that to then execute more javascript before executing another mysql script via an external php.

    Basically I want it to do this ->

    Code:
    function()
    {
        Send value through to php with xmlhttp
        return variable1
    
        if(variable1 == true)
        {
             run some more javascript stuff
        }
    
        send off another xmlhttp using variable1 as well as some other stuff passed into the function
    
    }
    I'm not sure if this makes sense but I am having trouble because (I think) the second xmlhttp doesn't wait for the first one to return variable1 so when it fires variable1 is undefined.. I also tried the approach of having the first xmlhttp in a seperate function and calling that but it seemed to have the same problem.

    Any help would be much appreciated!

    ps this is the code I am using:

    Code:
        if(window.XMLHttpRequest)
        {
    	xmlhttp = new XMLHttpRequest();
         }
        else
        {
    	xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
        }
    				
        xmlhttp.onreadystatechange = function()
        {
    	if(xmlhttp.readyState == 4 && xmlhttp.status == 200)
    	{
    	    variable1= xmlhttp.responseText;					
    	}
        }
    				
        xmlhttp.open("GET", "../phpurl.php?variable1="+variable1, true);
        xmlhttp.send();

  2. #2
    Join Date
    Feb 2014
    Location
    Managua, Nicaragua
    Posts
    45
    Work with Ajax instead XMLHttpRequests... Just a recommendation...

  3. #3
    Join Date
    Mar 2012
    Posts
    40
    hi!! I am not sure, but I think what you want is nested AJAX requests. This code could be useful
    Code:
    $.ajax({
                url: "../phpurl.php?variable1=" + variable1,
                success: function(result)
                {
                    if (result)
                    {
                        $.ajax({
                            url: "../phpurl.php?variable2=" + result,
                            success: function(result)
                            {
                                //do something else
                            },
                            type: 'GET'
                        });
                    }
                },
                type: 'GET'
            });
    By the way, why don't you use JQuery? As you can see is much easier than pure JavaScript.


    Let me know if this is what you are looking for

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles