duanmengmiezen8855 2015-03-19 10:12
浏览 107
已采纳

在文本框中插入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

  • 写回答

1条回答 默认 最新

  • dongpu3792 2015-03-19 10:41
    关注

    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);
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 需要跳转番茄畅听app的adb命令
  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证