[UPHPU] javascript call to ajax to php back to javascript

Merrill Oveson moveson at gmail.com
Wed Mar 9 16:36:51 MST 2011


Uphpers:

>From javascript I launch a php script via ajax.
so far so good.

the php script produces one single value.

how do I get this value back to javascript?

Here's my ajax:

 function getServerText(myurl,div_id) {
     var http = getXMLHTTPRequest();
     //alert ("php file is " + myurl);
     //alert ("div_id is " + div_id);
     myRand = parseInt(Math.random()*99999999999999999999999999999);
     var modurl = myurl+"rand="+myRand;
     http.open("GET", modurl, true);
     //alert ("readystate is " + http.readyState);
     http.onreadystatechange = function() {
        //alert ("div_id is " + div_id);
        if (http.readyState == 4) {
           if (http.status == 200) {
              var mytext = http.responseText;
              document.getElementById(div_id).innerHTML = mytext;
           }
        } else {
           //document.getElementById(div_id).innerHTML = 'please wait';
           //Remember your actually in the directory that launches
ajax.js not the includes directory!
           document.getElementById(div_id).innerHTML = '<img
src="images/anim.gif">';
        }
     }
     http.send(null);
 }


here's my javascript:

 var answer = getServerText(url,'a');

//answer ends up undefined.

php:

I've tried echo and return - no love.

Someone suggested JSON.  I only have one value so this seems like
overkill.  Or do I have to use JSON?
If so, do I create the JSON object in the php, then echo it?

Is there something wrong with my ajax script?

Thanks

Merrill


More information about the UPHPU mailing list