Javascript 搜索结果不显示在同一窗口中
我的搜索结果不显示在同一窗口上,我希望结果显示在同一窗口上。我发现了相同的问题,但代码与我使用的不同,因此我无法与之关联: 情景1: 如果我输入Javascript 搜索结果不显示在同一窗口中,javascript,php,html,Javascript,Php,Html,我的搜索结果不显示在同一窗口上,我希望结果显示在同一窗口上。我发现了相同的问题,但代码与我使用的不同,因此我无法与之关联: 情景1: 如果我输入action=“search\u result2.php”,它将在另一个页面上重定向结果 情景2: 如果我在下面的代码中使用了action=“,它什么也不做 <html xmlns="http://www.w3.org/1999/xhtml"> <head> <script> $(document).ready(fu
action=“search\u result2.php”
,它将在另一个页面上重定向结果
情景2:
如果我在下面的代码中使用了action=“
,它什么也不做
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script>
$(document).ready(function(){
$("#results").show();
});
</script>
<script type="text/javascript">
$(document).ready(function(){
$("#search").on('click',function() {
var find = $('#find').val();
var field = $('#field').val();
$.post('search_result2.php',{find:find, field:field}, function(data){
$("#results").html(data);
});
return false;
});
});
</script>
</head>
<body>
<div id="container" style="width:auto">
<div id="mainContent">
<h2>Search</h2>
<form name="search" method="post" action="">
Seach for: <input type="text" name="find" id="find" /> in
<Select NAME="field" id="field">
<Option VALUE="testA">A</option>
<Option VALUE="testB">B</option>
<Option VALUE="testC">C</option>
<Option VALUE="testD">D</option>
</Select>
<input type="hidden" name="searching" value="yes" />
<input type="submit" name="search" id="search" value="Search" />
</form>
<div id="results">
</div>
</div>
</div>
</body>
</html>
$(文档).ready(函数(){
$(“#结果”).show();
});
$(文档).ready(函数(){
$(“#搜索”)。在('单击',函数()上{
var find=$('#find').val();
变量字段=$(“#字段”).val();
$.post('search_result2.php',{find:find,field:field},函数(数据){
$(“#结果”).html(数据);
});
返回false;
});
});
搜寻
搜索:在
A.
B
C
D
以下是我的搜索结果2.php:
<?php
//This is only displayed if they have submitted the form
if (isset($_POST['searching']) && $_POST['searching'] == "yes")
{
echo "<h2>Results</h2><p>";
//If they did not enter a search term we give them an error
if (empty($_POST['find']))
{
echo "<p>You forgot to enter a search term";
exit;
}
// Otherwise we connect to our Database
mysql_connect("host", "username", "passw") or die(mysql_error());
mysql_select_db("testdb") or die(mysql_error());
// We preform a bit of filtering
$find = strtoupper($_POST['find']);
$find = strip_tags($_POST['find']);
$find = trim ($_POST['find']);
$field = trim ($_POST['field']);
//Now we search for our search term, in the field the user specified
$data = mysql_query("SELECT * FROM testtable WHERE upper($field) LIKE'%$find%'");
//And we display the results
while($result = mysql_fetch_array( $data ))
{
echo $result['testA'];
echo " ";
echo $result['testB'];
echo "<br>";
echo $result['testC'];
echo "<br>";
echo $result['testD'];
echo "<br>";
echo "<br>";
}
//This counts the number or results - and if there wasn't any it gives them a little message explaining that
$anymatches=mysql_num_rows($data);
if ($anymatches == 0)
{
echo "Sorry, but we can not find an entry to match your query<br><br>";
}
//And we remind them what they searched for
echo "<b>Searched For:</b> " .$find;
}
?>
如果要在同一页面中加载而不刷新页面,则需要发出ajax请求
如果可以重新加载页面,php部分必须与原始链接位于相同的“位置”。
例如,如果您将该代码与表单放在同一个文件的顶部(并使用.php扩展名重命名),它应该可以工作(如果php可以在该文件夹中进行解释)。我不是jQuery的专家,但为什么您有两个$(document).ready(function(){}
?第一个用于div结果
;第二个用于脚本在同一页上发布结果。如果有任何错误,请检查控制台。我不知道你的意思…但我没有收到任何错误。。。