call php script from javascript and get php script's output back in javascript

I am not an experienced java script php developer. I was doing my programming assignment( a google map mashup)  and now i m stuck at this.
Question is : "How can i call a php script from javascript passing a javascript variable as argument and then get back the ouput produced by the php script in a javascript variable?"

Note that I do not want any user activity in performing this except loading of the javascript/html page itself. after loading javascript/html page it should automatically call the php script in the background and put result in the required variable. I do not want to click on a link after opening the javascript page  and then start the whole process of calling php script.

Hope this helps, Please try to be fast .......i m already too late.
Eagerly waiting for ur help
Thanks in advance for ur time and response.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

This is, by definition, AJAX (Asyncronous Javascript and XML). You will submit a background request, via Javascript, to the remote server. The remote server will process your request via PHP, and return the results in XML (or other format of your choice).

Unfortunately, AJAX is sufficiently complex that I will not post an example here since I do not know your specific requirements. You can find a number of good tutorials via your favorite search engine.
namo21Author Commented:
Let me clear you the problem.

The php script( say "geturl.php" )  is on the same server/host as javascript/html page. Moreover geturl.php is a simple php script which  takes a url and downoad its content parse it to get required string and then output it through echo.

Now I want to  call this geturl.php from javascript passing url as argument and want to get its output back in some javascript variable.

should i post the code??
Use an php-AJXA-Framework like

include './xajax/xajax_core/';

$xajax = new xajax();

$rqstAdd =& $xajax->register(XAJAX_FUNCTION, 'calc');


function calc($a)
    $response = new xajaxResponse();
    $response->script('myvar=' . ($a+5));
    return $response;
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Here is a good place to start:

Basically you will create an XMLHttpRequest or XML ActiveXObject (for Firefox/Safari/Opera and MS IE, respectively) which will open a communication channel to your server. You provide the URL of the script you wish to execute as a parameter of the request/object.

Data will be returned when the PHP script completes it execution, and you will need to parse and display it as needed via your Javascript code.

A few more tutorials and examples:

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
namo21Author Commented:
HI glcummins,

thanx for ur help. That proved sufficient.
But I have this another problem while using the code from the tutorials.
plaese observe the code

---------------------------javascript code------------------------
<script type="text/javascript">

function setLatLong()
        if(output ==null)
        {alert("warning: place is :"+output); }
                alert("sending request for: "+output);

function setLatLongCallback(response)
                if (!response || response.Status.code != 200)
                        alert("Sorry, Google was unable to find that place:"+ output);
                } else
                        var feed = new google.feeds.Feed("");
                                if (!result.error)
                                        for (var i = 0; i < result.feed.entries.length; i++)
                                                var entry = result.feed.entries[i];
                                                var attributes = ["title", "link", "publishedDate", "contentSnippet"];
                                                for (var j = 0; j < attributes.length; j++)
                                                {                                                      map.addOverlay(addNewsItem(output,response.Placemark[0].Point.coordinates[0],response.Placemark[0].Point.coordinates[1],entry["link"],entry["title"],entry["contentSnippet"],entry["publishedDate"]));


function ajaxFunction()
        var ajaxRequest;
                // Opera 8.0+, Firefox, Safari
                ajaxRequest = new XMLHttpRequest();
            } catch (e){
                // Internet Explorer Browsers
                                ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
                        } catch (e) {
                                        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){
                        output  = ajaxRequest.responseText;         <----------- stroring returned value
                        alert("Value returned is:"+output);                    
        }"GET", "", true);


    ajaxFunction();                                                        <----------calling ajaxFunction here
    alert("value of output after ajaxfuncton: "+output);              <--------- printing value of var "output"
    setLatLong();                                                           <-------------- this function will use output value

-----------------------------Code Ends-------------------------------------------------------------------------
 this code works fine in explorer. but firefox has some problem.

The problem is that when i call "ajaxFunction" it seems the execution of main program(this script) does not halt or wait until ajaxFunction is returned and it continues with function "setLatLaong". so when i print the value of variable "output" it gives me "undefined". This is not the case with explorer 7.0.

Any suggestions why is this going this way????
namo21Author Commented:
sorry ......little more
When I run this in firefox first I will output --->

Value of after ajaxfunction : undefined
Warning: place is :undefined

then i get this line:
Value returned is: Patna                     <---------------this is the value correclty returned from the php script

thats is setLatLong function starts before ajaxFunction is returned.
namo21Author Commented: does not even work correcly in explorer........I mean execution order is arbit......sometime it does work .........other times it doesnt.
can i control the excution usig control statements i.e. wait while "output" variable is undefined and then excute setLatLong.
namo21Author Commented:
Got it ............i need to request data from php script synchronously rather than asynchronously.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.