0
function ajaxFunction(){
var ajaxRequest;  // The variable that makes Ajax possible!

try{
    // Opera 8.0+, Firefox, Safari
    ajaxRequest = new XMLHttpRequest();
} catch (e){
    // Internet Explorer Browsers
    try{
        ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
        try{
            ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e){
            // Something went wrong
            alert("Your browser is too old to run me!");
            return false;
        }
    }
}
// Create a function that will receive data sent from the server
ajaxRequest.onreadystatechange = function(){
    if(ajaxRequest.readyState == 4){
$.post('userfind.php', function(data) {

$("#resultTXT").val(data);

var response = data;

var parsedJSON = eval('('+response+')');
alert('parsedJSON:'+parsedJSON);

var result=parsedJSON.result;

var count=parsedJSON.count;

alert('result:'+result+' count:'+count);




},'json'

);      }
}
    ajaxRequest.open("POST", "userfind.php", true);
    ajaxRequest.send(null); 
}

Blockquote so I have got my above code, with your guys help. the above code will fill out the txtbox with a string, but i am not able to access each element of the string. The string is an array paged from a PHP file using Jsone_encode.

the array is something like this. [{"user_id":"2790","freelancer_name":"","order_id":"9121","orderamount":"0.00"

What I wanna do is to write a code like this :

    document.getElementById("_proId").value = user_id;
document.getElementById("_buyerSt").value = freelancer_name;
document.getElementById("_buyerDesc").value = order_id;
document.getElementById("_mngSt").value = orderamount;
  ... etc

Blockquote so my problem is how to split the string and fetch the data. these 2 vars

var result=parsedJSON.result;

    var count=parsedJSON.count;
    alert (""+result);
    alert (""+count);

only alert me undefined.

Please help me to fetch the data from the string.

the array is fetched from a mysql table and it's big

1
  • Why don't you use JSON.parse()? Commented Jul 12, 2012 at 1:40

1 Answer 1

2

1 . Looks there is no good reason to use eval to parse json by yourself.

You can use proven JSON library as json2.js or JSON-js

2 . Your JSON [ {"user_id":"123", ... }, {... }, {...} ] will be parsed as Array.

Use for each to iterate objects in Array.

Sign up to request clarification or add additional context in comments.

1 Comment

Indeed (+1). Such libraries exist: and if the browser supports the JSON object, then it's built-in for a win-in. Although it should be noted that json2.js uses eval after a series of transformations .. "The parse method uses the eval method to do the parsing, guarding it with several regular expressions to defend against accidental code execution hazards. On current browsers, this file does nothing, prefering the built-in JSON object."

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.