[英]Joomla custom PHP, jQuery module
I have a cascade menu using PHP, jQuery and mysql. 我有一个使用PHP,jQuery和mysql的级联菜单。 It works like a charm, but if I try to import it in Joomla as an article (with a Joomla extension wich activates PHP code in articles) it won't work correctly.
它的工作原理就像一种魅力,但是,如果我尝试将其作为文章导入Joomla(具有Joomla扩展名,可以激活文章中的PHP代码),它将无法正常工作。 The problem is, I don't even know where to find the source of the problem... I can select the category, but whenever I do, the second level of the cascade dropdown menu (category -> type -> model is the order) won't load, actually it says 'Please wait...', and after a few seconds the select option will be blank.
问题是,我什至不知道从哪里找到问题的源...我可以选择类别,但是每当我这样做时,级联下拉菜单的第二层(类别->类型->模型就是订单)将不会加载,实际上会显示“请稍候...”,几秒钟后,选择选项将为空。 I tested it on localhost, only the cascade menu not in Joomla framework and it worked...
我在localhost上进行了测试,只有级联菜单不在Joomla框架中,并且可以正常工作...
I've got some files: 我有一些文件:
script.php -> handles the jQuery effects, also the dropdowns: 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 -> after user selects category, this should show the types in that category in the second menu. select_type.php->用户选择类别后,这应该在第二个菜单中显示该类别中的类型。
<?php
include "class.php";
echo $opt->ShowType();
?>
select_model.php -> same as select type, but it's under the type selection, so this is the last level of the cascade menu. select_model.php->与选择类型相同,但是它在类型选择下,因此这是级联菜单的最后一层。
<?php
include "class.php";
echo $opt->ShowModel();
?>
And finally, the class.php, which connects to the database where I fetch the datas from to load them in the select menus. 最后是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();
?>
A few points to go through: 需要注意的几点:
mysql_connect
is this method of connecting to a database is deprecated, nor is it secure. mysql_connect
是不建议使用的这种连接数据库的方法,也不安全。 JFactory::getDBO();
JFactory::getDBO();
method. Hope this helps 希望这可以帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.