0

I have json_encoded an array of a mysql result as shown here:

$allquery="SELECT * FROM wp_users";
$names = array();   
$allresult=mysql_query($allquery) or die(mysql_error()); ?>

<?php while($rows=mysql_fetch_array($allresult)){  

  $names[] = $rows['display_name']; 

}

print json_encode( array( 

  "res" =>  $names,
  "fvdfvv" => "sdfsd"

) 
);

My problem is... when it gets back to the client-side, I am unsure about how to display it. I try and alert the reply on success:

  success: function(result){
            var allresult = result.res
            $('#result').html(  allresult  );

            alert(allresult);
        //$("#notice_div").hide(); 

      }

But this doesnt return anything. In fact, the success handler doesnt even begin.

Any ideas?

Thanks

4 Answers 4

2

First check in console if request is made, and status. If path is bad in request will see a 404.

If status is 200 will be able to see what is returned. If nothing returned there is problem in php, can echo error handler

To loop over the resulting response array:

 success: function(response) {
    var html='';

    var names=response.res
    $.each(names, function( i, name){
       html+= "<li>" + name + "</li>";
    });
    $('#result').html(  html  );


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

Comments

1

I would check your making a JSON request with your request call, I know mootools can be funny about that not sure what framework your using is it jQuery?

I would use echo instead of print (buts thats my preference)

Do you get any results at all? have you alerted out "result"?

if your definitely sure the success handler isn't firing, the problem is elsewhere. Perhaps you can expand your post with more of your code.

7 Comments

No I know the success is working ok, the problem comes when I try and get the database result to show in client side. I am alerting an array right? So im unsure about how to parse the array in javascript.
ok. Well it will be a JSON Object. Have you got firefox / Chrome? Can you turn on your firebug / developer tools and instead of: alert(result), use console.log(result) and you should see your object returned in the console. That will give you a better idea of whats coming back
Ahh yes... its returning "undefined"
So... i guess I need to know how to access or loop through the returned data? becuase its in an array right?
Does "result" return undefined or "result.res"?
|
1

1).Make an alert message after the initializing the function of success. Make a return false statement. If it makes an alert message means your process is going fine else try to check your url and data sent in the ajax call.

2). If it makes alert then look over decoding the incoming json object data.

This will work fine for you!. Thanks.

Comments

0

Possibly you should use GET, not POST (if it remained so since your previous post). Then do what Hamza told you to do.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.