【问题标题】:two drop-down menus with connection两个带连接的下拉菜单
【发布时间】:2013-11-11 09:10:05
【问题描述】:

我正在使用 smarty 和 php。我有两个下拉菜单。此外,我有两个数据库,一个带有类别,另一个带有带有类别 ID 的新闻。其中一个菜单读取类别。当我从第一个菜单类别中选择时,我希望页面自动刷新并将该类别的新闻放入第二个下拉列表中。

<form method="post">
        <h3>Category of news</h3>

      <select name="categoriesForm" id="news_cat">   
        <option value="0"></option>  
        {foreach from=$categories item=i}                              
            <option value="{$i.id}"> {$i.name|stripslashes} </option>            
        {/foreach}    
      </select>

        <h3 style="position:absolute;left:500px; top:130px;">Name of news</h3>

        <select name="news" id="news_name" style="position:absolute;left:500px; top:190px;">   
        <option value="0"></option>  
        {foreach from=$news item=i}                              
            <option value="{$i.id}"> {$i.name|stripslashes} </option>            
        {/foreach}    
      </select>


  </form>

这是控制器:

function edit_news(){
                    $cat = $this->news->getCategoriesNews();
                    $this->assign('categories',$cat);


                    $selected_key = $_POST['categoriesForm'];



                    $news = $this->news->getNameNews($selected_key);
                    $this->assign('news',$news);

        }     

这就是模型

 function  getCategoriesNews(){
        return  $this->db->GetAll("SELECT id, name FROM categories ");
   }

   function getNameNews($category){
        return $this->db->GetAll("SELECT name,cat_id FROM news WHERE cat_id = '$category' ");
   }

【问题讨论】:

    标签: php html drop-down-menu smarty


    【解决方案1】:

    好吧,我已经提到了该过程的概述。我相信只要做很少的功课,你就能实现你的功能。

    1) 在categoriesForm 下拉列表中使用onChange 来调用Javascript / jQuery 函数。因此,每当您更改 categoriesForm 的值时,就会调用一个脚本函数。

    <select name="categoriesForm" id="news_cat" onChange="feed_secondary()">
    

    2) feed_secondary 获取 categoriesForm 的当前值并触发和 ajax 调用。
    3)在ajax调用中,传递当前类别的值。
    4) 在控制器中,处理 category_id 并将该类别的所有新闻作为响应发送。
    4) 在feed_secondary 中,处理响应并操作二级下拉列表news

    【讨论】:

    • 谢谢,我不知道“onChange”,我做了一点工作,谢谢
    猜你喜欢
    • 1970-01-01
    • 2021-09-06
    • 2021-10-26
    • 2015-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多