我在我的页面中有ajax函数从其发布的php加载数据。唯一的问题是数据不会从php页面返回。请查看并告诉我代码中是否有任何错误。任何帮助非常感谢。这一切都应该没有让人耳目一新。
$('#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));
}
}
?>
答案 0 :(得分:0)
您不能在查询中多次重复使用相同的:keyword
占位符,您必须为它们指定不同的名称。
if(!is_array($filter)){
require_once('Connections/conn.php');
$stmt = $conn->prepare( "SELECT * FROM happyhours
WHERE (city LIKE :city OR zipcode LIKE :zip)
AND cost = :filter");
}
else {
$filter = implode(', ', $_POST['filter']);
require_once('Connections/conn.php');
$stmt = $conn->prepare( "SELECT * FROM happyhours
WHERE (city LIKE :city OR zipcode LIKE :zip)
AND dayOfTheWeek LIKE :filter");
}
try{
$stmt->execute(array(':city' => $searchStr.'%',
':zip' => $searchStr.'%',
':filter' => $filter));
}
catch(PDOException $ex){
echo $ex->getMessage();
}