Etienne
Etienne

Reputation: 103

having difficulty with responseText in javascript

I am having a problem in passing variables from php to javascript using json basically the problem is that in my javascript i can debug and view the items in the responseText but i cant assign them to a variable or view them, i've tried also a single item but didnt manage any ideas about why this is happening.

function ajaxrequestDB() {
    var AJAX = null; // Initialize the AJAX variable.

    if (window.XMLHttpRequest) { // Does this browser have an XMLHttpRequest object?
        AJAX=new XMLHttpRequest(); // Yes -- initialize it.
    } 
    else { // No, try to initialize it IE style
        AJAX=new ActiveXObject("Microsoft.XMLHTTP"); // Wheee, ActiveX, how do we format c: again?
    } // End setup Ajax.

    if (AJAX==null){ // If we couldn't initialize Ajax...
    alert("Your browser doesn't support AJAX."); // Sorry msg.
    return false // Return false, couldn't set up ajax
    }
        AJAX.onreadystatechange = function() { // When the browser has the request info..
            if (AJAX.readyState==4 || AJAX.readyState=="complete") 
            { // see if the complete flag is set.
            callback(AJAX.responseText, AJAX.status); // Pass the response to our processing function
            } // End Ajax readystate check.
        }

    var url='http://localhost/Scripts/refresh.php'; 
    //var url='http://cpdtest.zzl.org/Scripts/hidemarker.php?Name='+myname; 
    AJAX.open("GET", url, true); // Open the url this object was set-up with.
    AJAX.send(); // Send the request.       
    alert(AJAX.responseText);
    var result = AJAX.responseText;

    eval(result);
    //alert(result);
}

From the above if i do a debug on AJAX.responseText i can see the returned data from my php file, but alert(AJAX.responseText) i can only see a blank alert window.

below is also my php file which reads from the db and sends the variables to the javascript.

<?php
header('Content-type: application/json');
//$con = mysql_connect("localhost","770132_admin","admin");
$con = mysql_connect("localhost","root","");

if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("cpd", $con);

$SQL = "SELECT name from markers";
$result = mysql_query($SQL);
while ($db_field = mysql_fetch_assoc($result)){

$data[]= $db_field;

}  
echo json_encode($data);

?>

Upvotes: 1

Views: 1237

Answers (2)

Maxim Krizhanovsky
Maxim Krizhanovsky

Reputation: 26699

You have set the ajax request to be asynchronous when you set the 'true' in the following command:

AJAX.open("GET", url, true);

Which means that ajax response will be ready only when AJAX.readyState==4. Thus, you must put the following code

    alert(AJAX.responseText);
    var result = AJAX.responseText;

    eval(result);

Must be inserted to here:

AJAX.onreadystatechange = function() { // When the browser has the request info..
            if (AJAX.readyState==4 || AJAX.readyState=="complete") 
            { // see if the complete flag is set.
              //YOUR CODE//
            callback(AJAX.responseText, AJAX.status); // Pass the response to our processing function
            } // End Ajax readystate check.
        }

Upvotes: 1

Andrew D.
Andrew D.

Reputation: 8220

Try to use next javascript code:

function ajaxrequestDB(callback) {
    var AJAX; // Initialize the AJAX variable.
    if (window.XMLHttpRequest) { // Does this browser have an XMLHttpRequest object?
        AJAX=new XMLHttpRequest(); // Yes -- initialize it.
    }
    else if (window.ActiveXObject) { // No, try to initialize it IE style
        AJAX=new ActiveXObject("Microsoft.XMLHTTP"); // Wheee, ActiveX, how do we format c: again?
    } // End setup Ajax.

    if (!AJAX) { // If we couldn't initialize Ajax...
        alert("Your browser doesn't support AJAX."); // Sorry msg.
        return false; // Return false, couldn't set up ajax
    }

    AJAX.onreadystatechange = function() { // When the browser has the request info..
        if (AJAX.readyState==4) { // see if the complete flag is set.
            callback(AJAX.responseText, AJAX.status); // Pass the response to our processing function
        } // End Ajax readystate check.
    }
    var url='http://localhost/Scripts/refresh.php';
    //var url='http://cpdtest.zzl.org/Scripts/hidemarker.php?Name='+"myname";
    AJAX.open("GET", url, true); // Open the url this object was set-up with.
    AJAX.send(null); // Send the request.       
}

function showResult(text, status){
    if(status==200){
        alert(text);
        var result=JSON.parse(text);
        alert(result);
    }
    else alert("HTTP Error:" + status);
}

ajaxrequestDB(showResult);

Upvotes: 0

Related Questions