由PHP数组填充的Ajax(ify)Combox
我仍在学习Ajax是如何工作的,因此我在使用当前的PHP组合框并使其成为Ajaxified时遇到了很多困难。组合框选项由PHP数组填充,表示我希望在页面上显示的图像数量。现在所有的代码都在一个PHP文件中,尽管我很确定在Ajax中,它必须在两个页面中 哦,如果您能使用jQuery,我们将不胜感激由PHP数组填充的Ajax(ify)Combox,php,ajax,jquery,combobox,Php,Ajax,Jquery,Combobox,我仍在学习Ajax是如何工作的,因此我在使用当前的PHP组合框并使其成为Ajaxified时遇到了很多困难。组合框选项由PHP数组填充,表示我希望在页面上显示的图像数量。现在所有的代码都在一个PHP文件中,尽管我很确定在Ajax中,它必须在两个页面中 哦,如果您能使用jQuery,我们将不胜感激 <?php $curPage = 0; if(isset($_GET['page'])){ $curPage = (int) $_GET['page'];
<?php
$curPage = 0;
if(isset($_GET['page'])){
$curPage = (int) $_GET['page'];
}
// values of combobox in an array
$imgNum_values = array('12','16','20');
if(isset($_GET['imgs']) && in_array($_GET['imgs'], $imgNum_values))
{
$selected_imgNum = $_GET['imgs'];
}else{
// input default value, if empty the first variable will be shown
$selected_imgNum = '';
}
$option_num = count($imgNum_values);
echo '
<form name=imgNum method="get" action="new_arrivals_img.php">
<label>number of images per page:</label>
<select name="imgs" onChange="imgNum.submit();">';
for($x = 0; $x < $option_num; $x++)
{
// print the options
echo '
<option value="'.$imgNum_values[$x].'"'.($imgNum_values[$x] == $selected_imgNum ?
' selected="selected"' : '').'>'.$imgNum_values[$x].'</option>';
}
echo '
</select>
<input type="hidden" name="page" value="<?php echo $curPage; ?>" />
</form>';
?>
下面是显示图像的PHP查询等。我现在不需要将下面的部分更改为Ajax,除非将上面的代码更改为Ajax是必要的
<?php
if((int) $_GET['imgs'] > 0){
$limit = (int) $_GET['imgs'];
} else {
$limit = 12;
}
$mysql_link = mysql_connect("localhost", "root", "root");
mysql_select_db("new_arrivals_imgs") or die("Could not select database");
$query = mysql_query("SELECT `imgURL`,`imgTitle` FROM `images` ".
"ORDER BY `imgDate` DESC LIMIT " . $limit * $curPage . ", $limit") or die(mysql_error());
if(!$query) {
echo "Cannot retrieve information from database.";
} else {
while($row = mysql_fetch_assoc($query)) {
echo "<li><a href='new_arrivals_img/".$row['imgURL']."' class='gallery' title='".$row['imgTitle']."'><img src='new_arrivals_img/thumbnails/".$row['imgURL']."'></a></li>";
}
}
?>
非常感谢您的帮助。我将给您一个关于如何使用jQuery实现这一点的示例: HTML: 我假设您知道将jQuery代码放在哪里 PHP(get_images.PHP):
顺便说一句,我没有测试这个,我希望你能找到一些有用的东西那么你想让别人为你重写这些吗?那太棒了!但我想这可能需要很多工作。我想我要找的更多的是我必须做的事情的指南。由于组合框选项是由php创建的,我想我需要使用一些jQuery在加载时执行此操作,然后使用一些jQuery提交onChange。但是我不知道如何写这些函数等等。谢谢!我现在正在工作,所以我希望今晚回家后能测试一下,如果不行的话,明天早上。如果我对此有任何疑问,你可能会在我身边吗?好吧,如果你在这里留下另一条消息,我应该会收到电子邮件通知,巴勃罗,所以我已经测试过了,虽然组合框似乎运行正常,但我不能确定,因为数据(本例中的图像)没有显示。我要试着自己去弄清楚,虽然我不确定我知道我在做什么。我弄明白了。javscript
var value=sel.value()
应该是var value=sel.val()
。不过,我想知道别的事情。我希望将这段代码与jqueryajax分页代码集成,该代码已经组装好并可以使用。这有多困难?我应该把它放进去,它应该能工作吗?我对Ajax的工作原理了解不够,无法理解这一点。例如,php必须在单独的文件中吗?我不知道这是否会是一个问题,因为两者都共享公共信息,例如当前页面和每页的图像。
<form>
<label>Images Number:</label>
<select id="imgNum" name="imgNum">
<option value="12">12</option>
<option value="16">16</option>
<option value="20">20</option>
</select>
</form>
<div id="imgTray"></div>
//Bind the onChange event to Fetch images on combox selection
$("#imgNum").change(function(){
//The combo box
var sel = $(this);
//Selected value
var value = sel.value();
//Feth the images
$.get("get_images.php",{imgs: value}, function(data){
//Add images(or what ever the script output is) to the document
$("#imgTray").html(data);
});
})
//You should store the current selected option in a cookie
//For the sake of the example i'll set the default permanently to 12
var imgNum_selected = 12;
//set the initial selected option and trigger the event
$("#imgNum [value='"+imgNum_selected+"']")
.prop("selected","selected")
.change();
<?php
if((int) $_GET['imgs'] > 0){
$limit = (int) $_GET['imgs'];
} else {
$limit = 12;
}
$mysql_link = mysql_connect("localhost", "root", "root");
mysql_select_db("new_arrivals_imgs") or die("Could not select database");
$query = mysql_query("SELECT `imgURL`,`imgTitle` FROM `images` ".
"ORDER BY `imgDate` DESC LIMIT " . $limit * $curPage . ", $limit") or die(mysql_error());
if(!$query) {
echo "Cannot retrieve information from database.";
} else {
while($row = mysql_fetch_assoc($query)) {
echo "<li><a href='new_arrivals_img/".$row['imgURL']."' class='gallery' title='".$row['imgTitle']."'><img src='new_arrivals_img/thumbnails/".$row['imgURL']."'></a></li>";
}
}
?>