两个带连接的下拉菜单

时间:2013-11-11 09:10:06

标签: php html drop-down-menu smarty

我使用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' ");
   }

1 个答案:

答案 0 :(得分:0)

好吧,我已经提到了这个过程的概述。我相信,通过很少的功课,您将能够实现您的功能。

1)在onChange下拉列表上使用categoriesForm来调用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