我有一个使用 PHP、jQuery 和 mysql 的级联菜单。它就像一个魅力,但如果我尝试将它作为一篇文章导入 Joomla(使用 Joomla 扩展来激活文章中的 PHP 代码),它将无法正常工作。问题是,我什至不知道在哪里找到问题的根源......我可以选择类别,但每当我这样做时,级联下拉菜单的第二级(类别 -> 类型 -> 模型就是order) 不会加载,实际上它说“请稍候...”,几秒钟后选择选项将为空白。我在 localhost 上对其进行了测试,只有级联菜单不在 Joomla 框架中并且它有效......
我有一些文件:
script.php -> 处理 jQuery 效果,还有下拉菜单:
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.10.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("select#type").attr("disabled","disabled");
$("select#model").attr("disabled","disabled");
$("select#category").change(function(){
$("select#type").attr("disabled","disabled");
$("select#type").html("<option>Please wait...</option>");
var id = $("select#category option:selected").attr('value');
$.post("select_type.php", {id:id}, function(data){
$("select#type").removeAttr("disabled");
$("select#type").html(data);
});
});
$("select#type").change(function(){
$("select#model").attr("disabled","disabled");
$("select#model").html("<option>Please wait...</option>");
var id2 = $("select#type option:selected").attr('value');
$.post("select_model.php", {id2:id2}, function(data){
$("select#model").removeAttr("disabled");
$("select#model").html(data);
});
});
$("select#model").change(function(){
var cat = $("select#category option:selected").attr('value');
var type = $("select#type option:selected").attr('value');
var model = $("select#model option:selected").attr('value');
if(cat>0 && type>0 && model >0)
{
var model = $("select#model option:selected").html();
var type = $("select#type option:selected").html();
$("#result").html('<br>Your choice: ' + type + ' ' + model + '.');
}
else
{
$("#result").html("<br>One of the inputs is empty!");
}
return false;
});
});
</script>
<form id="select_form">
Choose category: <select id="category">
<?php echo $opt->ShowCategory(); ?>
</select><br />
Choose type: <select id="type">
<option value="0">Please select...</option>
</select>
<br />
Choose model: <select id="model">
<option value="0">Please select...</option>
</select></form>
<div id="result"></div>
<br><br>
select_type.php -> 用户选择类别后,这应该在第二个菜单中显示该类别中的类型。
<?php
include "class.php";
echo $opt->ShowType();
?>
select_model.php -> 与选择类型相同,但在类型选择之下,所以这是级联菜单的最后一级。
<?php
include "class.php";
echo $opt->ShowModel();
?>
最后是 class.php,它连接到我从中获取数据以将它们加载到选择菜单中的数据库。
<?php
class SelectList
{
protected $conn;
public function __construct()
{
$this->DbConnect();
}
protected function DbConnect()
{
$host = "localhost";
$user = "root";
$password = "usbw";
$db = "test";
$this->conn = mysql_connect($host,$user,$password) OR die("error!");
mysql_select_db($db,$this->conn) OR die("error!");
return TRUE;
}
public function ShowCategory()
{
$sql = "SELECT * FROM categories";
$res = mysql_query($sql,$this->conn);
$category = '<option value="0">Please select a category...</option>';
while($row = mysql_fetch_array($res))
{
$category .= '<option value="' . $row['id_cat'] . '">' . $row['name'] . '</option>';
}
return $category;
}
public function ShowType()
{
$sql = mysql_query( "SELECT * FROM type WHERE id_cat=$_POST[id]");
$res = mysql_query($sql,$this->conn);
$type = '<option value="0">Please select a type...</option>';
while($row = mysql_fetch_array($sql))
{
$type .= '<option value="' . $row['id_type'] . '">' . $row['name'] . '</option>';
}
return $type;
}
public function ShowModel()
{
$sql = "SELECT * FROM model WHERE id_model=$_POST[id2]";
$res = mysql_query($sql,$this->conn);
$model = '<option value="0">Please select a model...</option>';
while($row = mysql_fetch_array($res))
{
$model .= '<option value="' . $row['id_model'] . '">' . $row['name'] . '</option>';
}
return $model;
}
}
$opt = new SelectList();
?>