2015-05-09 71 views
0

我有三个php文件,它们是header.php,search.php和index.php。在header.php中,我发送ajax POST请求到search.php进行实时搜索。 header.php包含在index.php文件中。当我只运行header.php时,ajax代码工作正常,但在执行index.php时不起作用。我的代码如下:当文件被包含在另一个文件中时,AJAX POST不工作

的header.php:

<script> 
function showResult(str,str2) { 
    if (str.length==0) { 
    document.getElementById("livesearch").innerHTML=""; 
    document.getElementById("livesearch").style.border="0px"; 
    return; 
    } 
    else 
    { alert("fff"); 

    var url = 'search.php'; 
    $.post(url,{str:str,str2:str2},function(data){ 

    document.getElementById("livesearch").style.border="1px solid #DDD"; 
document.getElementById("livesearch").style.backgroundColor="#A5ACB2"; 
document.getElementById("livesearch").innerHTML=data; 
    }); 
    } 

} 
</script> 

********************************************************************88 

的search.php:

<?php 
mysql_connect("localhost","root",""); 
    mysql_select_db("buynsell"); 
$text=$_POST["str"]; 
$category=$_POST["str2"]; 
$results=''; 

$query=("SELECT title from productdesc where title LIKE '$text%' AND category_name='".$category."' "); 

    $result = mysql_query($query); 
    while($row = mysql_fetch_object($result)) { 
     $results.=$row->title."<br>"; 
    } 
    echo $results; 
?> 

**********************************************8 

的index.php:

在这里,我包括header.php中

<body> 
<div class="container" style="font-size:14px"> 

<?php include '../common/header.php'; ?> 
+0

你得到的错误是什么? –

+0

显示没有错误..控制转到header.php中的其他语句,因为警报消息显示..但如果我把警报消息后发表声明它不显示 –

+0

您可以检查您的服务器的错误日志? –

回答

0

事情证明,问题在于search.phpindex.php被假定位于同一个文件夹中,但它们位于不同的文件夹中。对服务器错误日志的一瞥立即引导了解决方案,通过这个简单的步骤就可以解决问题,而无需任何额外的帮助。

相关问题