Click here to Skip to main content
13,664,996 members
Rate this:
 
Please Sign up or sign in to vote.
See more:
I want to retrieve multiple rows from my mysql database with Ajax, Javascipt, PHP but I seem to be running into problem. My codes are below:

<?php

include"connect.php";

$user_id=$_POST['user'];

$query = "SELECT fname,sname FROM info WHERE userid = '$user_id' ";
  $result = mysql_query($query);
   
  while ($row = mysql_fetch_array($result)){
   
  

    $fname= $row['fname'];
	
    $sname= $row['sname'];
 
   

    $results[] = array("sname" => $fname, "fname" => $sname);

  }
  header('Content-type:application/json');
  exit(json_encode($results)); 
  
  ?>



<script>


$(function(){
    // Don't worry about this

    $.ajax({
		
                                      
      url: 'list.php',                  //the script to call to get data          
      data: "?user=5778", //5778 is a sample data to pass to list.php
                                       
      dataType: 'json',                //data format      
      success: function(data)          //on recieve of reply
      {

		
		
		 var html, comment;
            for(var i = 0; i < data.length; i++) {
                comment = data[i];
                html += '<td>'+ comment.id + '</td><td>' + comment.vname + '</td><td> details</td>'
            }
            $('#output1').html(html);
		
		
	  }
});
});

</script>



Please I need help. I want it to automatically generate a table on the page that is, each result on a new row. thanks
Posted 3-Jul-13 1:50am

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

If you want a record on each row, so add the <tr> element:
html += '<tr><td>'+ comment.id + '</td><td>' + comment.vname + '</td><td> details</td></tr>'
  Permalink  
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web01-2016 | 2.8.180810.1 | Last Updated 3 Jul 2013
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100