Parsing JSON Data From MySQL Table to Different Divs

Parsing JSON Data From MySQL Table to Different Divs

Having a jQuery Ajax request from MySQL database I know how to render the data in an HTML element format like tables or list as below:

  1. $.ajax({
  2.                 type:"POST",
  3.                 url:"appdata.php",
  4.                 success: function (newdata) {
  5.                     $('#result').html(newdata);
  6.                 }
  7.        });
The PHP as:

  1. $mysqli = new mysqli('localhost', 'root', '******', '********');
  2. $resultStr = '';
  3. $query = 'SELECT * FROM appusers';
  4. if ($result = $mysqli->query($query)) {
  5.  if ($result->num_rows > 0) {
  6.         $resultStr.='<ul>';
  7.          while($row = $result->fetch_assoc()){
  8.           $resultStr.= '<li>'.$row['fname'].' - '.$row['lname'].' - '.$row['phone'].'</li>';
  9.         }
  10.          $resultStr.= '</ul>';
  11.         }
  12.         else {
  13.                $resultStr = 'Nothing Found';
  14.              }
  15.     }
  16. echo $resultStr;
  17. ?>
The other option, also, is passing the result as JSON by using the json_encode() as below

  1.    $mysqli = new mysqli('localhost', 'root', '******', '********');
  2.    $query = 'SELECT * FROM appusers';
  3.    $result = $mysqli->query($query)->fetch_array();
  4.    echo json_encode($result);
Now I am totally confused how to use this json_encode($result) in the Ajax call? For example can you please let me know if I want render the result of the query in series of divs like

  1. <div id"fname"></div>
  2. <div id"lname"></div>
  3. <div id"phone"></div>
how can I parse this in ajax success property? and associuate each field on related div? Thanks for you help in advanced.