0

My PHP web page is returning a JSON string. I wrote following function to get those data and display them on jQuery mobile listview.

function LoadJsonDataFunction()
{  
  $.getJSON("my_web_page.php", function(obj) {
    $.each(obj, function(key, value){
      $("ul").append("<li>"+value.fname+"</li>");
    });
  });
}

Here is my listview code:

<ul data-role=listview> </ul>

I have called to the function in the body tag

<body onload="LoadJsonDataFunction()">

but when I executing the program it displays "undefine" and no data.

then I change $.getJSON() request like this.then its working perfectly.

$.getJSON("some_page_returning_same_json_string.json",function(obj) { .....

let me know how can i fix this.

PS. Here is my php page output..

{
  "employees":[
    {
      "fname": "sdsdsd",
      "lname": "sdsd",
      "phone": "sdsd",
      "gender": "female",
      "dob": "1990-03-11",
      "address": "03",
      "nic": "erer",
      "email": "erererer",
      "empid": "ererere",
      "designation": "sdsds",
      "qualifications": "dsds"
    }
  ]
}

Here is my php code

<?php
  header('Content-Type: application/json');
  /*
    Following code will list all the employees
  */

  // array for JSON response
  $response = array();

  // include db connect class
  require_once __DIR__ . '/db_connect.php';

  // connecting to db
  $db = new DB_CONNECT();

  // get all employees from employees table
  $result = mysql_query("SELECT * FROM emp_master") or die(mysql_error());

  // check for empty result
  if (mysql_num_rows($result) > 0) {
    // looping through all results
    // employees node
    $response["employees"] = array();

    while ($row = mysql_fetch_array($result)) {
      // temp user array
      $employee = array();
      $employee["fname"] = $row["fname"];
      $employee["lname"] = $row["lname"];
      $employee["phone"] = $row["phone"];
      $employee["gender"] = $row["gender"];
      $employee["dob"] = $row["dob"];
      $employee["address"] = $row["address"];
      $employee["nic"] = $row["nic"];
      $employee["email"] = $row["email"];
      $employee["empid"] = $row["empid"];
      $employee["designation"] = $row["designation"];
      $employee["qualifications"] = $row["qualifications"];

      //push single employee into final response array
      array_push($response["employees"], $employee);
    }
    // success
    // $response["success"] = 1;
    // echoing JSON response

    echo json_encode($response);
  } else {
    // no employees found
    $response["success"] = 0;
    $response["message"] = "No employees found";

    // echo no users JSON
    echo json_encode($response);
  }
?>
3
  • how are you returning json data from PHP end ...? Commented Sep 25, 2013 at 9:55
  • 1
    Are you returning json string with json_encode/json_decode from php? Commented Sep 25, 2013 at 9:57
  • Did you tried to add another foreach in your javascript ? Commented Sep 25, 2013 at 10:04

3 Answers 3

3

Have you write correct header? If not write this as first line in your PHP:

header('Content-Type: application/json');
Sign up to request clarification or add additional context in comments.

1 Comment

yes..i have added header after your comment.but still displaying the previous "undefined" item on the list.
2

Change the line:

 $.each(obj, function(key, value){

with

 $.each(obj.employees, function(key, value){

The "employees" contains in "obj" and then it contains the array. "obj" does not contain the array you are looking.

Comments

0

This is the best exemple i used to begin JQM , you can check the link or use the code below

$('#ListPage').bind('pageinit', function(event) {
    $.getJSON('some_php_or_json_file_link', function(data) {
        $('#listView li').remove();
        $.each(data.items, function(index, item) {
            $('#listView').append('<li><span>' + item.json_label1 + '</span></li>');
        });
        $('#listView').listview('refresh');
    });
});

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.