可能重复: How我可以刷新一个细节选择列表时,我的主人使用选择的变化AJAX
我只是在工作一段时间后,又走了学习PHP和环顾四周后,认为我需要用JS / Ajax来做到这一点,我打算更多地了解后,我得到更加舒适用PHP。我想学习如何做到这一点的一些事情我工作了。
我有我显示为一个链接列表的父项的表格。当父项被点击我想点击的父的子项显示在另一份名单。我可以得到2列表中显示简单的查询,我只是不知道如何让页面/ SQL查询,点击时更新。
< PHP的要求(connection.inc.php'); ?>
< DIV ID =名单>
< H3>列表< / H3>
< PHP
$名单=请求mysql_query(SELECT * FROM表)
或死亡(mysql_error());
而($信息= mysql_fetch_array($清单))
{
回声&所述; A HREF = \#\>中$信息['LISTNAME']。&所述; / a取代;&所述峰; br />中;
}
?>
< / DIV>
< DIV ID ='listItems中的'>
< H3>的名单,其中,?PHP的$父= 2;回声$父&GT?;项目< / H3>
< PHP
$时listItems =请求mysql_query(SELECT * FROM listItems中的WHERE父母= $父)
或死亡(mysql_error());
而($信息= mysql_fetch_array($ listItems中))
{
回声$信息['了itemname'。< BR />中;
}
?>
< / DIV>
解决方案
code一边(mysql_ * == MUCHO德precato)的问题,您需要检查,如果该请求是AJAX和输出时listItems。
我用jQuery来简化Ajax请求。
< PHP
需要('connection.inc.php');
/ * AJAX请求* /
如果(空($ _ SERVER ['HTTP_X_REQUESTED_WITH'])及!&安培;
用strtolower($ _ SERVER ['HTTP_X_REQUESTED_WITH'])=='xmlhtt prequest')
{
$查询= sprintf的(SELECT * FROM listItems中的WHERE父=%d的,
mysql_real_escape_string($ _ REQUEST ['父']));
$时listItems =请求mysql_query($查询)
或死亡(mysql_error());
的printf(< H3>清单%D项目< / H3>',$ _REQUEST ['父']);
而($信息= mysql_fetch_array($ listItems中))
{
回声$信息['了itemname'。< BR />中;
}
出口;
}
/ *正常的请求* /
?>
< DIV ID =名单>
< H3>列表< / H3>
< PHP
$名单=请求mysql_query(SELECT * FROM表)
或死亡(mysql_error());
而($信息= mysql_fetch_array($清单))
{
回声&所述; A HREF = \#\>中$信息['LISTNAME']。&所述; / a取代;&所述峰; br />中;
}
?>
< / DIV>
< DIV ID ='listItems中的'>
< / DIV>
&LT;脚本的src =// ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
&LT;脚本&GT;
jQuery的(功能($)){
$('#名单)。代表('A','点击',函数(){
$('#listItems中)的负载(window.location.pathname,{父:$(本)的.text()})。
返回false;
});
}
&LT; / SCRIPT&GT;
Possible Duplicate: How can I refresh a detail select list when my master select changes using AJAX
I'm just working learning PHP again after some time away and after looking around think I need to use JS/ajax to make this happen which I intend to learn more about after I get more comforable with PHP. I would like to learn how to do this for some thing I am working on now.
I have a table of parent items which I display as a list of links. When a parent item is clicked I want the child items of the clicked parent to be displayed in another list. I can get the 2 lists to display with simple queries I just don't know how to get the page/sql query to update when clicked.
<?php require ('connection.inc.php'); ?>
<div id="lists">
<h3>Lists</h3>
<?php
$lists = mysql_query("SELECT * FROM lists")
or die(mysql_error());
while($info = mysql_fetch_array( $lists ))
{
echo "<a href=\"#\">".$info['ListName']."</a><br />";
}
?>
</div>
<div id='listitems'>
<h3>List <?php $parent=2; echo $parent?> Items</h3>
<?php
$listitems = mysql_query("SELECT * FROM listitems WHERE parent=$parent")
or die(mysql_error());
while($info = mysql_fetch_array( $listitems ))
{
echo $info['itemName']."<br />";
}
?>
</div>
解决方案
Code issues aside (mysql_* == mucho deprecato), you need to check if the request is AJAX and output listitems.
I used jquery to simplify the ajax request.
<?php
require ('connection.inc.php');
/* AJAX request */
if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) &&
strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest')
{
$query = sprintf('SELECT * FROM listitems WHERE parent=%d',
mysql_real_escape_string($_REQUEST['parent']));
$listitems = mysql_query($query)
or die(mysql_error());
printf('<h3>List %d Items</h3>', $_REQUEST['parent']);
while($info = mysql_fetch_array( $listitems ))
{
echo $info['itemName']."<br />";
}
exit;
}
/* Normal request */
?>
<div id="lists">
<h3>Lists</h3>
<?php
$lists = mysql_query("SELECT * FROM lists")
or die(mysql_error());
while($info = mysql_fetch_array( $lists ))
{
echo "<a href=\"#\">".$info['ListName']."</a><br />";
}
?>
</div>
<div id='listitems'>
</div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script>
jQuery(function($)){
$('#lists').delegate('a', 'click', function(){
$('#listitems').load(window.location.pathname, {parent: $(this).text()});
return false;
});
}
</script>