javascript - 获取数据并使用ajax将其显示在文本框中

标签 javascript php mysql ajax

正如您所看到的,我当前的代码不起作用,我需要你们的帮助...我想在我的搜索(文本框)中进行过滤并显示在相应的文本框中过滤的数据...任何人都可以帮助我的代码工作??我已经在谷歌上寻找了几个小时,寻找与我的程序相同的想法,但我找不到任何帮助。

预期输出示例:

enter image description here

html代码:

<form method="post">
Search batchcode: <input type="text" id="query" /><br />
<table>
<tr>
<td>
ID: <br />
<input id="result" type="text" name="id1" /> <br />
<input id="result" type="text" name="id2" /> <br />
</td>
<td>
Name: <br />
<input id="result" type="text" name="name1" /> <br />
<input id="result" type="text" name="name2" /> <br />
</td>
<td>
Score 1: <br />
<input id="result" type="text" name="optA1" /> <br />
<input id="result" type="text" name="optA2" /> <br />
</td>
<td>
Score 2: <br />
<input id="result" type="text" name="optB1" /> <br />
<input id="result" type="text" name="optB2" /> <br />
</td>
<td>
Other Qualification: <br />
<input id="result" type="text" name="other_qual1" /> <br />
<input id="result" type="text" name="other_qual2" /> <br />
</td>
<td>
Interview: <br />
<input id="result" type="text" name="interview1" /> <br />
<input id="result" type="text" name="interview2" /> <br />
</td>
<td>
Total: <br />
<input id="result" type="text" name="total1" /> <br />
<input id="result" type="text" name="total2" /> <br />
</td>
</tr>
</table>
</form>

脚本函数:

<script type="text/javascript">
$(document).ready(function(){

$('input[name^=search]').click(function(e){
e.preventDefault(); 
$.ajax({
url:"search.php",
type:"POST",
data : { term : $('#query').val() },
dataType:json,
success : function(result) {
alert(result);
            }
        });
    })
});      
</script>

search.php 页面:

<?php

$q = $_GET['term'];

mysql_connect("localhost","root","");
mysql_select_db("test");
$query = mysql_query("SELECT * FROM score WHERE batchcode LIKE '$q%'");

$data = array();
while($row = mysql_fetch_array($query)){
$data[]=array('value'=>$row['batchcode']);
        $id[] = $row['id'];
        $name[] = $row['name'];
        $score1[] = $row['score1'];
        $score2[] = $row['score2'];
        $other_qual[] = $row['other_qual'];
        $interview[] = $row['interview'];
        $total[] = $row['total'];
}
echo json_encode($data);
?>

最佳答案

没有任何“搜索”按钮可以单击处理程序。 尝试在搜索输入后添加按钮,如下所示:

Search batchcode: <input type="text" id="query" /><br />
<input type="button" id="search" value="Search"/>

关于javascript - 获取数据并使用ajax将其显示在文本框中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21488900/

相关文章:

php - 如何重定向到设置为不活动的选项卡式页面

php - 使用 Javascript 添加一个选择框;到由 PHP 填充的 html 表单

c# - 如何将 DropDownList 的更改保存到 sql 数据库?

javascript - 如何防止第二次输入相同的字符

javascript - 单击事件 jQuery 后更改默认图像

php - 使用 PHP_XLSXWriter 设置列宽

php - 如何在wamp中安装5.2版本的PHP

mysql - 通过自检提取特定数据

javascript - 如何在javascript中同步使用setTimeout?

javascript - 图片随机播放