Click here to Skip to main content
16,018,650 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
<?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)); 
  
  ?>



JavaScript
<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

1 solution

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

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900