我认为最好的方法是使用 jQuery(一个 JavaScript 库)。 I 非常易于使用,如果你掌握了诀窍,你可以用它做出惊人的事情。
对于 PHP/MySQL,您可以使用 jQuerys Ajax 功能(请参阅http://api.jquery.com/jQuery.ajax/)。使用回调显示加载的数据(见下文)。
这是一个非常简单的示例,说明如何使用动态内容显示另一个 div(其中可能是更多可供选择的链接)。如果你把它和 Ajax 结合起来,你应该得到你所需要的。
在 head 标签中包含 jQuery:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js" type="text/javascript"></script>
正文代码:
<!-- First Box: click on link shows up second box -->
<div id="selectOne" style="float: left; margin-right: 10px; border: #666 thin solid; padding: 10px;">
<a href="#" id="one">One</a><br />
<a href="#" id="two">Two</a><br />
<a href="#" id="three">Three</a>
</div>
<!-- Second Box: initially hidden with CSS "display: none;" -->
<div id="selectTwo" style="float: left; margin-right: 10px; display: none; border: #666 thin solid; padding: 10px;"></div>
<!-- The JavaScript (jQuery) -->
<script type="text/javascript">
//Do something when the DOM is ready:
$(document).ready(function() {
//When a link in div with id "selectOne" is clicked, do something:
$('#selectOne a').click(function() {
//Fade in second box:
$('#selectTwo').fadeIn(500);
//Get id from clicked link:
var id = $(this).attr('id');
//Depending on the id of the link, do something:
if (id == 'one') {
//Insert html into the second box which was faded in before:
$('#selectTwo').html('One<br />is<br />selected')
} else if (id == 'two') {
$('#selectTwo').html('Two<br />is<br />selected')
} else if (id == 'three') {
$('#selectTwo').html('Three<br />is<br />selected')
}
});
});
</script>
因此,如果您要使用 jQuerys Ajax-Functionality,您可以使用类似的东西(未经测试!):
$('#selectOne a').click(function() {
var id = $(this).attr('id');
$.ajax({
type: 'POST',
url: 'getYourData.php',
data: 'thisIsSentToPHPFile='+id,
success: function(msg){
//everything echoed in your PHP-File will be in the 'msg' variable:
$('#selectTwo').html(msg)
$('#selectTwo').fadeIn(500);
}
});
});
getYourData.php 可能是:
$id = $_POST['id'];
$query = mysql_query('SELECT * FROM table WHERE id='.$id);
$result = mysql_fetch_assoc($query);
//Now echo the results - they will be in the callback variable:
echo $result['tablefield1'].', '.$result['tablefield2'];
试一试,稍微调整一下,你应该就能让它工作了。