简体   繁体   English

使用 PHP 和 Ajax 检索数据

[英]Retrive data using PHP and Ajax

Here I am trying to fetch data from Mysql database using PHP and Ajax .在这里,我尝试使用PHPAjaxMysql数据库中获取数据。 My problem is, when I type the Test ID on test_id text box , just it immediatetly shows the Test Name according to the Test ID on test_name text box .我的问题是,当我在test_id text box键入Test ID时,它会根据test_name text boxTest ID立即显示Test Name

Here I used the function called checkname .这里我使用了function称为checkname In console just it show the Test Name but does not show in the test_name text box .在控制台中它只显示Test Name但不显示在test_name text box

Here is the HTML code.这是HTML代码。

<table class="table table-hover table-white">
 <thead>
  <tr>
    <th class="col-sm-1">Test ID</th>
    <th class="col-md-6">Test Name</th>
    <th style="width:100px;">Amount</th>
    <th> Action</th>
  </tr>
 </thead>
 <tbody id="rows">
   <tr>
     <td> <input class="form-control" type="text" style="width:200px" id="test_id[]" onblur="checkname();" onkeyup="checkname();" onchange="checkname();"> </td>
     <td> <input  type="text" style="width:300px" class="form-control text-right form-amt"  readonly="" id="test_name[]" > </td>
     <td> <input  type="text" style="min-width:100px" class="form-control text-right form-amt" readonly="" id="amount[]"> </td>
     <td><center> <a href="javascript:void(0)" class="text-success font-18" title="Add" id="add"><i class="fa fa-plus"></i></a> </center> </td>
   </tr>
 </tbody>
</table>

Here is the Ajax code;这是Ajax代码;

<script>

function checkname()
{
var test_id = document.getElementById("test_id[]").value;
$.ajax({
       type: 'post',
       url: "adminquery/fetch_test_name.php", // request file the 'check_email.php'
       data: {'test_id': test_id, },
       success: function (data) {
           $("#test_name[]").html(data);
         }
        });
           }
</script>

Here is the PHP code这是PHP代码

<?php
include('../auth/dbconnection.php');
$output='';
$sql="SELECT * from testings where test_id='".$_POST['test_id']."'";
$result=mysqli_query($conn,$sql);
while($row=mysqli_fetch_array($result)){
$name= $row["testing_name"];
  $output.='    <input  type="text"  readonly="" id="test_name[]" value="'.$name.' "> '.$name.'   ';
}
echo $output;
?>

Anyone could help me may highly appreciated.任何人都可以帮助我可能会非常感激。

First lose the brackets in the id.先去掉id中的括号。

<input id="test_name" type="text" value="other" />

Then use .val() instead of .html()然后使用.val()而不是.html()

$("#test_name").val("something")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM