-1
我的网站上有一个画廊,当用户从下拉菜单中选择一个选项(选项代表位置)时需要更改。AJAX php gallery
目前我有以下两种选择:
<select class="form-control" id="photomenu">
<option value="Beckmouth Area">Beckmouth Area</option>
<option value="Barras Square area">Barras Square area</option>
</select>
我的jQuery的选择选择的值,并将其发送到我的AJAX请求,像这样:
<script>
$(document).ready(function() {
$("#photomenu").change(function(){
var option = $("#photomenu option:selected").val();
var url = "access_database.php";
$.post(url, option, function(data)
{
$("#staithes_gallery").html(data)
});
});
});
</script>
我access_database.php
样子:
<?php
$folder_location = $_GET["option"];
$path = "photos_staithes/Thumbnails/";
//connect to staithesbooks table
$servername = "localhost";
$username = "***********";
$password = "***********";
$dbname = "*********";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql_select_photo = "SELECT * FROM `staithesphotos` WHERE `folder` = '$folder_location'";
$results = $conn->query($sql_select_photo);
if ($results->num_rows > 0) {
// output data of each row
while($row = $results->fetch_assoc()) {
//create a list of all names and folder locations
$name[] = $row["name"];
}
//create the photo gallery
for ($x=0; $x<count($name); $x++)
{
$photo_gallery .= "<div class='col-md-4 work-grid animated wow slideInLeft'>
<a href='$path$folder_lcation$name[$x]' data-lightbox='featured' data-title='' style='cursor:pointer'><div class='item'>
<img src='$path$folder_location/$name[$x]' height='431' width='594' title='name' />
<div class='caption' style='display: none;'>
<h2>Some Title</h2>
<p>This is a caption to end all captions</p>
</div>
</div>
</a></div>";
}
};
echo $photo_gallery;
?>
我已经测试PHP
的工作原理是为$folder_location
提供一个静态文件位置。
在执行AJAX call
后,我希望用新库更新#staithes_gallery
中的html。
通过测试并将结果输出到警告框,我只需接收对象Object。我不确定我做错了什么。
任何帮助将不胜感激!
等等,这里你真正的问题是什么?你无法得到PHP返回的请求或$'folder_location'值或查询的任何问题? – DontVoteMeDown
我不确定问题出在哪里。 jquery只是返回对象Object而不是新的HTML文本。我已经单独测试了每个组件,并且似乎可行。我不确定这个值是不是正确的PHP或从PHP返回的数据是错误的 – Jimmybob
设置'$ sql_select_photo'后'把'echo $ sql_select_photo;退出;'并直接在浏览器中调用URL。然后在你的数据库中检查你的查询。 – DontVoteMeDown