0
我有和ajax函數在我的頁面加載數據從它發佈到的PHP。唯一的問題是數據不會從php頁面返回。請看一看,並告訴我在代碼中是否有任何錯誤。任何幫助非常感謝。而且這一切都應該沒有令人耳目一新。php不返回數據來查詢ajax函數
$('#filterByPrice button', '#filterByPrice button').click(function(e){
e.preventDefault();
var form = $(this).closest('form');
$.ajax({
url: 'filter-data.php' ,
type: 'POST',
data: form.serialize(),
success: function(data){
$('.container').html("");
$('.container').html(data);
}
});
});
而且PHP文件是:
<?php
if(isset($_POST['filter'])){
$searchStr = $_SESSION['searchString'];
$filter = $_POST['filter'];
if(!is_array($filter)){
require_once('Connections/conn.php');
$stmt = $conn->prepare("SELECT * FROM happyhours
WHERE (city LIKE :keyword OR zipcode LIKE :keyword)
AND cost = '$filter'");
}
else {
$filter = implode(', ', $_POST['filter']);
require_once('Connections/conn.php');
$stmt = $conn->prepare("SELECT * FROM happyhours
WHERE (city LIKE :keyword OR zipcode LIKE :keyword)
AND dayOfTheWeek LIKE '$filter'");
}
try{
$stmt->execute(array(':keyword' => $searchStr.'%'));
}
catch(PDOException $ex){
echo $ex->getMessage();
}
$count = $stmt->rowCount();
$each = $stmt->fetch(PDO::FETCH_ASSOC);
if ($count > 0){
do {
echo '<table id="results" width="700px" border="0">
<tr>
<td rowspan="5" width="130" id="photo"><a class="lightbox" href="'.$each['imageURL'].'"><p>'.$each['name'].'</p><img id="hh-image" src="'.$each['imageURL'].'" width="80" height="80" /></a></td>
<tr><td id="hh-name" style="word-wrap:break-word; font-size:16px; font-family:\'Myriad Pro\'; font-weight:500" width="560" height="20"><'.$each['name'].'</td></tr>
<tr><td> <a href="'.$each['googleMap'].'" target="new" style="font-size:14px">'.$each['address'].'</a></td>
</tr>
<tr>
<td style="word-wrap:break-word; font-size:14px; font-family:\'Myriad Pro\'" height="20">'.$each['phone'].'</td>
</tr>
<tr>
<td style="word-wrap:break-word; font-size:14px; font-family:\'Myriad Pro\'" height="20">'.$each['dayOfTheWeek'].' ('.$each['hours'].')</td>
</tr>
</table>';
} while($each = $stmt->fetch(PDO::FETCH_ASSOC));
}
}
?>
您的查詢返回任何結果?你檢查了嗎? –
是回報什麼期望 –
僅供參考,你不需要'.html(「」)'。第二個'.html()'完全取代了HTML。 – Barmar