2015-05-09 30 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被假定位於同一個文件夾中,但它們位於不同的文件夾中。對服務器錯誤日誌的一瞥立即引導瞭解決方案,通過這個簡單的步驟就可以解決問題,而無需任何額外的幫助。

相關問題