简体   繁体   English

Joomla自定义PHP,jQuery模块

[英]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: 需要注意的几点:

  1. don't use mysql_connect is this method of connecting to a database is deprecated, nor is it secure. 不要使用mysql_connect是不建议使用的这种连接数据库的方法,也不安全。
  2. There is no need to manually connect to a database. 无需手动连接到数据库。 You can use the JFactory::getDBO(); 您可以使用JFactory::getDBO(); method. 方法。 More information on that can be found here 有关更多信息,请参见此处
  3. I'm not sure how you're adding this custom code to your article, but I hope you realise that you should be usign a plugin such as Sourcerer 我不确定如何将自定义代码添加到文章中,但是我希望您意识到自己应该使用Sourcerer这样的插件
  4. Rather than adding all this code into your article, I think it might be better off making a creating a custom module. 我认为与其创建所有自定义模块,不如将所有这些代码添加到您的文章中。 By that I actually mean developing one and using you're code. 实际上,我的意思是开发一个并使用您的代码。 It won't take too long. 不会花太长时间。 then you can embed the module into you article. 然后您可以将该模块嵌入到您的文章中。
  5. Rather than importing jQuery using the tags, please refer to my answer on the best method of importing it using Joomla coding standards here 而不是使用标签导入jQuery的,请参考我的答案上使用Joomla编码标准导入它的最好方法, 在这里
  6. If you haven't already looked at the Joomla Extensions Directory , I suggest you do so, as there are lots of menu modules that your could install and edit to your own liking. 如果您尚未查看Joomla扩展目录 ,我建议您这样做,因为您可以根据自己的喜好安装和编辑许多菜单模块。

Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM