使用jquery从搜索结果中自动填充

时间:2015-10-14 22:25:20

标签: php jquery mysqli

您好我有以下代码,用user-id,User_mname,email

填充搜索结果

我想自动填充id = id的字段,并在

上单击相应的Id用户
    <?php 

    include '../db/connect6.php';


if(isset($_POST['searchVal'])) {
  $searchq = $_POST['searchVal'];
  $searchq = preg_replace ("#[^0-9a-z]#i","",$searchq);

    $query = mysql_query("SELECT * FROM oz2ts_users WHERE oz2ts_users.id LIKE  '%$searchq%' OR oz2ts_users.name LIKE '%$searchq%'") or die("Could not  search"); 
    $count = mysql_num_rows($query);
    if($count == 0){
     $output = 'There is no result to show!'; 
    } else{ 
     while($row = mysql_fetch_array ($query)) {
        $id = $row['id'];
        $name = $row['name'];
        $username = $row['username'];    

    $output .= '<div><a class="resultItem" data-id="' . $id . '">'   
     . $name . ' '.$username.'</a></div>';   

   }            
 }

 }
echo($output);
?>

1 个答案:

答案 0 :(得分:0)

只需执行以该字段为目标的.onclick()事件即可。示例代码如下。

<form>
   <input id="display-id" type="text" />
</form>

<script>
$(document).ready(function(){
    $('.resultItem').click(function(){
         var resultId = $(this).data('id');
         $('#display-id').val(resultId);
    });    
});
</script>