Php 需要AJAX帮助吗
我最近偶然发现了一个小问题。基本上,我试图让我的用户能够通过我的网站上的某个类别进行搜索,但搜索是通过AJAX完成的,不会重新加载页面,只会加载正在搜索的内容 下面的代码是我到目前为止想到的,但它唯一会改变的时间是文本框中没有任何值,只是内容没有更新(我手动检查了PHP文件,没有错误&对于Firefox的HTTP Direct插件,我确保调用了我的PHP文件) 我的代码: category.php:Php 需要AJAX帮助吗,php,ajax,search,Php,Ajax,Search,我最近偶然发现了一个小问题。基本上,我试图让我的用户能够通过我的网站上的某个类别进行搜索,但搜索是通过AJAX完成的,不会重新加载页面,只会加载正在搜索的内容 下面的代码是我到目前为止想到的,但它唯一会改变的时间是文本框中没有任何值,只是内容没有更新(我手动检查了PHP文件,没有错误&对于Firefox的HTTP Direct插件,我确保调用了我的PHP文件) 我的代码: category.php: <script type="text/javascript"> functi
<script type="text/javascript">
function showCat(str, cat)
{
if (str=="")
{
document.getElementById("showCategory").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("showCategory").innerHTML=xmlhttp.responseText;
}
}
url="../cat_search.php?q="+str+"&cat="+cat
xmlhttp.open("GET",url,true);
xmlhttp.send();
}
</script>
<input type="text" name="showCat" onkeyup="showCat(this.value, <?=$id?>)" style="width: 140px;" />
<div id="showCategory">
## Stuff is being listed here on the load of the page & then should be updated with Ajax
</div>
函数showCat(str,cat)
{
如果(str==“”)
{
document.getElementById(“showCategory”).innerHTML=“”;
返回;
}
if(window.XMLHttpRequest)
{//IE7+、Firefox、Chrome、Opera、Safari的代码
xmlhttp=新的XMLHttpRequest();
}
其他的
{//IE6、IE5的代码
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}
xmlhttp.onreadystatechange=函数()
{
if(xmlhttp.readyState==4&&xmlhttp.status==200)
{
document.getElementById(“showCategory”).innerHTML=xmlhttp.responseText;
}
}
url=“../cat_search.php?q=“+str+”&cat=“+cat
open(“GET”,url,true);
xmlhttp.send();
}
使用jqueryforajax。认真地这是非常简单和痛苦的。那是什么
$q=$_GET["q"];
$search = $q;
$q = "%".$q."%";
为什么不呢
$q = '%'.$_GET['q'].'%';
例如,jQuery中的代码:
<script type="text/javascript">
$(document).ready(function(){
$('input[name=showCat]').keyup(function(){
showCat($(this).val(),$(this).attr('id'));
});
});
function showCat(str,cat) {
if (str == '') {
$('#showCategory').html('');
return;
} else {
$.get('../cat_search.php',{q:str,cat:cat},function(data){
$('#showCategory').html(data);
});
}
}
</script>
<input type="text" name="showCat" id="<?=$id?>" style="width: 140px;" />
<div id="showCategory">
## Stuff is being listed here on the load of the page & then should be updated with Ajax
</div>
$(文档).ready(函数(){
$('input[name=showCat]').keyup(function(){
showCat($(this.val(),$(this.attr('id'));
});
});
函数showCat(str,cat){
如果(str=''){
$('#showCategory').html('');
返回;
}否则{
$.get('../cat_search.php',{q:str,cat:cat},函数(数据){
$('#showCategory').html(数据);
});
}
}
你能使用Firebug(Firefox插件)查看AJAX请求发生时的情况吗?这会给你Js错误,并告诉你AJAX请求何时发送和何时不发送。如果你搜索,
,会发生什么情况?我在我的计算机上进行了测试;您在这里发布的代码没有任何问题(除了它对SQL注入攻击非常开放)。您是否能够使用HTTP Direct验证您的PHP是否返回了某些内容?您是否检查了服务器的错误日志以查找任何致命错误?我是jQuery的超级粉丝。然而,这个答案无助于解决问题。他的JavaScript是正确的,运行良好!
<script type="text/javascript">
$(document).ready(function(){
$('input[name=showCat]').keyup(function(){
showCat($(this).val(),$(this).attr('id'));
});
});
function showCat(str,cat) {
if (str == '') {
$('#showCategory').html('');
return;
} else {
$.get('../cat_search.php',{q:str,cat:cat},function(data){
$('#showCategory').html(data);
});
}
}
</script>
<input type="text" name="showCat" id="<?=$id?>" style="width: 140px;" />
<div id="showCategory">
## Stuff is being listed here on the load of the page & then should be updated with Ajax
</div>