Javascript 在php的文本框中显示过滤后的数据
我想在文本框中的数组中显示过滤后的数据……我已经在谷歌上搜索了好几天了,想知道我的程序是否可行,但我找不到任何帮助 正如你们所看到的,我目前的代码不起作用,我需要你们的帮助 预期产出示例: html代码:Javascript 在php的文本框中显示过滤后的数据,javascript,php,html,ajax,json,Javascript,Php,Html,Ajax,Json,我想在文本框中的数组中显示过滤后的数据……我已经在谷歌上搜索了好几天了,想知道我的程序是否可行,但我找不到任何帮助 正如你们所看到的,我目前的代码不起作用,我需要你们的帮助 预期产出示例: html代码: <form name="form" method="get"> Search batchcode: <input type="text" id="query" name="search" /><br /> <table> <tr&g
<form name="form" method="get">
Search batchcode: <input type="text" id="query" name="search" /><br />
<table>
<tr>
<td>
ID: <br />
<input id="id1" type="text" name="id1" /> <br />
<input id="id2" type="text" name="id2" /> <br />
</td>
<td>
Name: <br />
<input id="name1" type="text" name="name1" /> <br />
<input id="name2" type="text" name="name2" /> <br />
</td>
<td>
Score 1: <br />
<input id="optA1" type="text" name="optA1" /> <br />
<input id="optA2" type="text" name="optA2" /> <br />
</td>
<td>
Score 2: <br />
<input id="optB1" type="text" name="optB1" /> <br />
<input id="optB2" type="text" name="optB2" /> <br />
</td>
<td>
Other Qualification: <br />
<input id="other_qual1" type="text" name="other_qual1" /> <br />
<input id="other_qual2" type="text" name="other_qual2" /> <br />
</td>
<td>
Interview: <br />
<input id="interview1" type="text" name="interview1" /> <br />
<input id="interview2" type="text" name="interview2" /> <br />
</td>
<td>
Total: <br />
<input id="total1" type="text" name="total1" /> <br />
<input id="total2" type="text" name="total2" /> <br />
</td>
</tr>
</table>
</form>
搜索批处理代码:
ID:
名称:
分数1:
得分2:
其他资格:
采访:
总计:
脚本代码:
<script type="text/javascript">
$(document).ready(function(){
$('input[name^=search]').click(function(){
$.ajax({
url:"search.php",
type:"GET",
data: { term : $('#query').val() },
dataType:JSON,
success: function(result) {
$('#id1').val(result.id).show();
$('#id2').val(result.id).show();
$('#name1').val(result.name).show();
$('#name2').val(result.name).show();
$('#optA1').val(result.score1).show();
$('#optA2').val(result.score1).show();
$('#optA1').val(result.score2).show();
$('#optA2').val(result.score2).show();
$('#other_qual1').val(result.other_qual).show();
$('#other_qual2').val(result.other_qual).show();
$('#interview1').val(result.interview).show();
$('#interview2').val(result.interview).show();
$('#total1').val(result.total).show();
$('#total2').val(result.total).show();
}
});
})
});
</script>
$(文档).ready(函数(){
$('input[name^=search]')。单击(函数(){
$.ajax({
url:“search.php”,
键入:“获取”,
数据:{term:$('#query').val()},
数据类型:JSON,
成功:功能(结果){
$('#id1').val(result.id).show();
$('#id2').val(result.id).show();
$('#name1').val(result.name).show();
$('#name2').val(result.name).show();
$('#optA1').val(result.score1.show();
$('#optA2').val(result.score1.show();
$('#optA1').val(result.score2.show();
$('#optA2').val(result.score2.show();
$('#other_qual1').val(result.other_qual.show();
$('#other_-qual2').val(result.other_-qual.show();
$('#interview1').val(result.interview.show();
$('#interview2').val(result.interview.show();
$('#total1').val(result.total.show();
$('#total2').val(result.total.show();
}
});
})
});
search.php页面:
<?php
$q = $_GET['term'];
mysql_connect("localhost","root","");
mysql_select_db("test");
header('Content-type: application/json');
$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.php
页面时,我得到以下输出:
$\u GET['term']代码>未定义
您需要这样做:
search.php?term=myTerm
在ajax回调函数中为元素分配相同的数据
您需要遍历从AJAX调用返回的数据,并将其分配给各个元素
for(i=0;i<data.length;i++){
//first loop for first set of records and second for second set of records
$(selector).val(data.attr);
}
用于(i=0;单击功能也可能会产生问题。当您单击字段时启动搜索,在您有机会在中键入内容之前,您应该只共享相关代码以保存问题。)resources@GroundZero那我该怎么办?你能帮帮我吗?@Khan Shahrukh你在说什么?这都是相关的你没读过吧?@xplodyonkeyup或onkeydown应该更好。我应该把它放在哪里。这只是一个代码示例。您应该在成功回调AJAX时使用它。