1
$result = mysql_query ("SELECT * FROM order_list");

$test = array();
$i=0;

$myjsons = array();
while($row = mysql_fetch_assoc($result)){ 
    echo $myjsons[] = json_encode(array($row));
}

and a AJAX javascript

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) {
            document.getElementById("resultTXT").value = eval("    (" + ajaxRequest.responseText + ")");
        }
    }
    ajaxRequest.open("POST", "userfind.php", true);
    ajaxRequest.send(null);
}​

the javascript function wont store the array into the textbox

can anyone tell me whats the problem here?

Thanks

3
  • echo $myjsons[] = json_encode(array($row)); What are you expecting from this to do? Commented Jul 10, 2012 at 13:11
  • 3
    You are building something in PHP that isn't going to be valid JSON. Accumulate it into an array: while ($row = mysql_fetch_assoc($result)){ $myjsons[] = $row; } Then encode as JSON and output it: echo json_encode($myjsons); Commented Jul 10, 2012 at 13:11
  • what does it prints on console.log ajaxRequest.responseText Commented Jul 10, 2012 at 13:12

2 Answers 2

1

try:

 while($row = mysql_fetch_assoc($result)){ 
     $myjsons[] = json_encode(array($row));
    }
 echo json_encode($myjsons);

instead of

while($row = mysql_fetch_assoc($result)){ 
  echo $myjsons[] = json_encode(array($row));
}

you can use jquery library for tha ajax call http://api.jquery.com/jQuery.post/

$.post('userfind.php', function(data) {
   $("#resultTXT").val(data);// ==document.getElementById("resultTXT").value =data;
},'json'

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

1 Comment

my next problem will be to evaluate the string and generate a JavaScript generated table LOOOL
1

JSON output would be invalid in your example, build the array and out put a single json encoded array like so:

$myjsons = array();
while($row = mysql_fetch_assoc($result)){ 
    $myjsons[] = $row;
}
echo json_encode($myjsons);

1 Comment

Thanks I have got the array parsed in javascript, I will have to go on to the next step generating the dynamic table

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.