在文本框中插入id后,单击使用ajax php sql在html表中输入填充数据

在文本框中插入id后,单击使用ajax php sql在html表中输入填充数据

问题描述:

I need to fill all matching records in a table after inserting id in textbox. But currently I am getting only one record.

php

<?php
 $id=$_POST['userID'];
 $db_host = 'jo\SQL2005';
 $db_username = 'jo';
 $db_password = '123321';
 $db_name = 'db_test2';
 mssql_connect($db_host, $db_username, $db_password);
 mssql_select_db($db_name); 
 $query=mssql_query("select * from address where user_id='$id'");
 $result=mssql_fetch_assoc($query);
$json= array('id'=>$result['id'],'user_id'=>$result['user_id'],
'street'=>$result['street'],
'quarter'=>$result['quarter'],'Phone_number'=>$result['Phone_number']);
 echo json_encode($json);
 exit;

?>

javaScript

function getname()
{
var id=$("#userID").val();    // get the id from textbox
$.ajax({
        type:"post",
        dataType:"json",
        data:"userID="+id,
        url:"address_json_data.php",   
        success:function(json)
        { 
          $("table.imagetable").
           append("<tr><td>" + json.user_id + "</td><td>" +
            json.street + "</td></tr>");
        }
        });
       }

html

<input name="userID" id="userID"  onChange="getname()" type="text" />
<table border='1'class="imagetable">
<tr><th>id</th><th>email</th></tr>
</table>

Any Help Very Thanks

replace this code:

$query=mssql_query("select * from address where user_id='$id'");
$result=mssql_fetch_assoc($query);
$json= array('id'=>$result['id'],'user_id'=>$result['user_id'],
'street'=>$result['street'],
'quarter'=>$result['quarter'],'Phone_number'=>$result['Phone_number']);

with this:

$query=mssql_query("select * from address where user_id='$id'");
while($result=mssql_fetch_assoc($query))
{
   $json[]= array(
                  'id'=>$result['id'],
                  'user_id'=>$result['user_id'],
                  'street'=>$result['street'],
                  'quarter'=>$result['quarter'],
                  'Phone_number'=>$result['Phone_number']
  );
}

And in your javascript:

success:function(json)
    { 
      var str = '';
      for(var x in json)
      {
         str += "<tr>";
         str += "<td>" + json[x].user_id + "</td>";
         str += "<td>" +json[x].street + "</td></tr>";
      }
      $("table.imagetable").append(str);
    }