0

show_page.php:

   <?

 session_start();
  include("includes/function.php");

$static = mysql_query("SELECT * FROM fb_pages WHERE user_id = '$_SESSION[user_id]' AND page_value = '1' ") or die(mysql_error());

            while($stat = mysql_fetch_array($static)) {

                    $page = $stat['page_name'];


                            echo json_encode($page);
  exit();

                    }       

ob_and_flush();     
?>  

and js code:

  <script type="text/javascript">

$.ajax({
type: "POST",
url: "show_page.php",
dataType: "JSON", //tell jQuery to expect JSON encoded response
success: function(response) {

 $('#page').html(response);
 }
});


 </script>  

I like show the result in <div id="page"></div> .

If run this code, the result is one page, and always one page, but if I delete in show_page.php , exit(); then result is good, show all page, but don't show webpage, I think, then may json. What this problem??

4
  • Please, DO NOT write new code with mysql_query. It's a deprecated interface that's being removed from PHP. Also, you should be very careful about properly escaping SQL values or you will end up with nasty SQL injection bugs. You should not be inserting arbitrary data from $_SESSION into your queries. Commented Jul 23, 2013 at 17:30
  • Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial. Commented Jul 23, 2013 at 17:31
  • Not sure what you're saying your problem is. Are you saying it's not giving you JSON? Commented Jul 23, 2013 at 17:32
  • The problem is, then don't show all page, just one! Always one page, but delete exit(), the code, then show all page, but this time don't use jquery, ajax :S Commented Jul 23, 2013 at 17:34

3 Answers 3

1

You're exiting in your loop, so it only processes the first row. You need to collect all the results in an array, call json_encode on the entire array, and then exit.

$page = array();
while ($stat = mysql_fetch_assoc($static)) {
  $page[] = $stat['page_name'];
}
echo json_encode($page);
ob_end_flush();
exit();

However, your jQuery does:

$('#page').html(response);

This expects response to be HTML, not an array. You need to change that to a loop that does something with each page name in the response, e.g.

$.each(response, function(i, p) {
   $('#page').append($('<div/>', { text: p }));
});
Sign up to request clarification or add additional context in comments.

6 Comments

Also, just to add to this ob_and_flush(); doesn't exist, it should be ob_end_flush(), or ob_end_clean().
There's probably no reason to use the ob_XXX functions anyway.
100% agree, after exiting, it would not attempt to run it, but just so OP is clear.
If you want help, you'll have to be more specific about what the problem is with my answer.
Naturally, I want, and changed ob_and_flush() on ob_end_clean(), but so not working. it does not matter if you show a different method, the goal would be to display the result of the php file with jquery and ajax
|
0

this part

 $page = $stat['page_name'];


 echo json_encode($page);

looks like $page is not array, so you don't need to add json_encode() function. Therefore, you don't need dataType: "JSON"

1 Comment

here is the problem, if I use jquery with no results then you can just run the show_page.php good
0

Looks like you're trying to output multiple INDEPENDENT json strings. This will not work. The code should probably be more like:

$data = array();
while ($stat = mysql_fetch_array($static)) {
    $data[] = $stat['page_name'];
}
echo json_encode($data);

Remember that JSON is basically the right-hand side of a javascript assignment operation. Anything you create as JSON MUST be valid javascript, e.g.

var foo = json_goes_here.

You're doing something like

var foo = 'bar''baz';

Instead, build an array of strings, which when json-encoded, will be more like

var foo = ['bar', 'baz'];

1 Comment

I don't understand what you mean, with JSON

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.