ajax: get variable from other page & put them into current page

Discussion in 'JavaScript' started by dracula51, Nov 24, 2009.

  1. #1
    Hey.
    i hav two page. suppose page.php & page_ajax.php
    in page.php there are some <input /> form field. & in page_ajax.php there are some JS variable declared (<script....>var xtitle = 'blah blkah'</script>)
    i want page.php to get those variable from page_ajax.php & put them into those input form's value

    i tried this...no luck :(
    var xmlHttp
    
    function getInfo()
    {
    	xmlHttp=GetXmlHttpObject();
    		if (xmlHttp==null)
      		{
    			alert ("Your browser does not support AJAX!");
    			return;
      		}
    	document.getElementById("getInfoButton").disabled = true;
    	document.getElementById("getInfoButton").value = 'Loading...';
    
    	var url="./vbv/page_ajax.php";
    	xmlHttp.onreadystatechange=stateChanged;
    	xmlHttp.open("GET",url,true);
    	xmlHttp.send(null);
    } 
    
    function stateChanged() 
    { 
    	if (xmlHttp.readyState==4)
    	{ 
    		document.getElementById("title").value = xtitle;
    		document.getElementById("tags").value = xtags;
    		document.getElementById("desc").value = xdesc;
    		document.getElementById("getInfoButton").disabled = false;
    	}
    
    }
    
    
    
    function GetXmlHttpObject()
    {
    	var xmlHttp=null;
    	try
    	{
    		xmlHttp=new XMLHttpRequest();
    	}
    	catch (e)
    	{
    	try
    		{
    			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    		}
    		catch (e)
    		{
    			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    		}
    	}
    
    	return xmlHttp;
    }
    Code (markup):
    xtitle, xtags, xdesc are the variables already declared in page_ajax.php

    i tried to grab info by clicking a button (id=getInfoButton & onclick=getInfo() )
     
    dracula51, Nov 24, 2009 IP
  2. camjohnson95

    camjohnson95 Active Member

    Messages:
    737
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #2
    You simply need to make page_ajax.php ouput the values of those variables: e.g (page_ajax.php):
    
    <script>
        document.write(xtitle + "," + xtags + "," + xdesc);
    </script>
    
    Code (markup):
    page.php:
    
                              var rText = xmlHttp.responseText.split(","); //splits the response by comma(,) and stores each result in an array
    		document.getElementById("title").value = rText[0];    //retrieves first result
    		document.getElementById("tags").value = rText[1];   //second
    		document.getElementById("desc").value = rText[2];  //third
    
    Code (markup):
    If this is not sufficient then you can output xml (from page-ajax.php) and then read the xml(from page.php)... whatever suits your needs.
     
    Last edited: Nov 25, 2009
    camjohnson95, Nov 25, 2009 IP
  3. dracula51

    dracula51 Peon

    Messages:
    146
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    tnx mate
    ...just like i want :)
     
    dracula51, Nov 25, 2009 IP