1

I have a problem, I want to fetch data from mysql using ajax and show it in one dropdown (select tag).

My problem is how do I send data to jQuery?

AJAX ja_drop.php

$query = "SELECT list_id,link,title FROM tbl_list ORDER BY title ASC;";

    $result = mysqli_query($dblink, $query);

    if (mysqli_num_rows($result) > 0) {
        // output data of each row
        while($row = mysqli_fetch_assoc($result)) {

            $results[] = $row;

        }
    } 

    print_r($results);

jQuery code

$.post("ja_drop.php")
.done(function(obj) {

    $("#ts56").text(obj);

// i want a loop statments for show data detail but not work any loop here

});

When I print the recive object to my jQuery code, it shows the following result:

[0] => Array (
    [list_id] => 25
    [link] => http://213.217.33.27/z_tree/znv3/test1.htm
    [title] => Ab Madani Damavand (WL-Tehran-Tehran Tower)(10.234.159.59)(1395925)
    )
[1] => Array (
    [list_id] => 35
    [link] => http://213.217.33.27/z_tree/znv3/test1.htm
    [title] => Ab Madani Damavand (WL-Tehran-Tehran Tower)(10.234.159.59)(1395925)
    )
[2] => Array (
    [list_id] => 36
    [link] => http://213.217.33.27/z_tree/znv3/test1.htm
    [title] => Ab Madani Damavand (WL-Tehran-Tehran Tower)(10.234.159.59)(1395925)
    )
[3] => Array (
    [list_id] => 37
    [link] => http://213.217.33.27/z_tree/znv1/test1.htm
    [title] => Ab Madani Damavand (WL-Tehran-Tehran Tower)(10.234.159.59)(1395925)
    )

As you see my above array have 4 index but when i want print array lenght show me wrong number

$("#ts56").text(obj.length);  // show 902
LinkinTED
  • 16,671
  • 4
  • 27
  • 53
hi lo
  • 67
  • 4
  • 3
    Possible duplicate of [Returning JSON from a PHP Script](https://stackoverflow.com/questions/4064444/returning-json-from-a-php-script) – Darragh Enright Jul 22 '19 at 10:32

2 Answers2

0

Use dataType option to accept the response in JSON format.

// -- ja_drop.php ----------
$query = "SELECT list_id,link,title FROM tbl_list ORDER BY title ASC";
$result = mysqli_query($dblink, $query);

if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {
        $results[] = $row;
    }
}
echo json_endcode($results);


// -- jquery code ----------
$.ajax({
    url: "ja_drop.php",
    type: "POST",
    dataType: "json",

    contentType: false,
    cache: false,
    processData:false,
    success: function(data)
    {
        console.log(data.length); // this will return 4 
        // process the data using loop
        $.each(data, function(i, e) {
            console.log(e.list_id);
            console.log(e.link);
            console.log(e.title);
        })
    },
    error: function() {}
});
Vinay Patil
  • 730
  • 6
  • 17
0

In ajax ja_drop.php return the response in json format using json_encode

echo json_encode($results);

In jquery parse the response with JSON.parse(), and the data becomes a JavaScript object.

jQuery code.

$(document).ready(function() {
    $.post("ja_drop.php") .done(function(obj) {

        //Parse the data with JSON.parse(), and the data becomes a JavaScript object.
        var data = JSON.parse(obj);

        //use each loop to get the data from response link, title etc
        $.each(data, function(index, value) {
           console.log("<a href='"+value.link+"' title='"+value.title+"'>"+value.title+"</a>");
        });
    });

 });
Shivendra Singh
  • 2,975
  • 1
  • 9
  • 11