Help solve the dilemma: trying to output data from the database on Ajax. JSON data array is formed, but I do not see any data on the page. Help me find a joint. Code:

<script type="text/javascript"> $(document).ready (function previuLoad (){ $.ajax ({ type: "POST", url: "php/previuLoad.php", dataType: "json", success: function (data){ $('#smenu').html(data); } }); }); </script> <div id="smenu"></div> <?php include 'db.php'; mysql_query("set names 'utf8'"); $result = array(); $res = mysql_query ("SELECT title,link FROM foot", $db); $arp = mysql_fetch_assos ($res); while ($arp = mysql_fetch_assos ($res)){ $result[] = $arp } echo json_encode ($result); ?> 

The array in the database has the form: (id: 1.2.3.et.d. title: title1. Title2.title3, etc.) and in that spirit.

I have a suggestion that I incorrectly refer to a multidimensional array. What should the query syntax look like, for example, the third Title record: data.2.title? Or I'm wrong?

  • after $result[] = $arp not enough semicolon. - etki
  • A dilemma is when there are several solutions in front of a person, each one that is arguable as the right option, as it misses the benefit of other options. In your case, this is just a task). What is the problem, people have already answered you). - IntegralAL

4 answers 4

  1. Error in this line. What assos ? It is necessary:

    assoc ? $arp = mysql_fetch_assos ($res);

  2. Expose the header before the line echo json_encode ($result); like this:

    header('Content-type: application/json');

  3. In javascript url: "php/previuLoad.php" . Is this the executable file?

  • In javascript url: "php / previuLoad.php", is this the executable file? Yes, this is the executable file. according to the first remark - it was banally sealed. Thanks, I will try - Alex

Apparently your mistake is that even before Json is issued, your script dumps the HTML code. Either move the PHP code above HTML, or change the dataType to "html" and then look at what the server returns.

P.S. And let's already put FireBug or use DeveloperTools to know what comes from the server. And then your script is waiting for JSON, and what is unknown in reality comes.

    To get data on Ajax you need

    1. URL, by visiting which, you can get the data.
    2. AJAX function that accesses this URL, it is desirable after all through the GET protocol, and not POST.

    The data in general from the server is given?

    • the script itself (run from the string directly) forms the json array. Whether it comes to the script - xs, firebug does not show an error. The data types tried different sequences too. Output without Json_encode simply in a loop (HTML data) returns what kind of cracks. - Alex
    • If you open this URL in the browser, something will be displayed on the page? - novus42

    Thank you so much ActivX !! The problem was in the php script. Right Option:

     <?php include 'db.php'; mysql_query("set names 'utf8'"); $result = array(); $i = 0; $res = mysql_query ("SELECT id,title,link FROM foot", $db); $arp = mysql_fetch_assoc ($res); do{ $result[$i] = $arp; $i++; } while ($arp = mysql_fetch_assoc ($res)); header('Content-type: application/json'); echo json_encode ($result); 

    ?>

    Thanks everyone who responded !!!