2015-08-05 05:20



I created a drop-down to select the category for search. When I search the product for example I search Shoes for MEN, when the view page loaded the item resets to default

I want the category to remain what I selected

  <form action="<?php echo Yii::app()->baseUrl; ?>/search" method="GET"  class="form-inline form-section-2 row  fadeInDown animated">
  <div class="col-sm-5 form-group">
  <input type="text"  name="loc"  class="form-control" id="loc" value="<?php  echo $locationdet ; ?>" placeholder="Enter Your Keyword">
  <div class="col-sm-4 form-group" >
     <select  name="cat" class="form-control selectpicker">
        <option>Select Category</option>
        <option value = '0'>Men</option>
        <option value = '1'>Women</option>          
  <div class="col-sm-3 form-group">
  <button type="submit" class="btn btn-default btn-new">Search Products</button>    
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答


  • doumen6532 doumen6532 6年前

    Try this:

    <option value = '0' <?php if(isset($_GET['cat']) && $_GET['cat'] == '0') echo "selected" ?>>Men</option>
    <option value = '1' <?php if(isset($_GET['cat']) && $_GET['cat'] == '1') echo "selected" ?>>Women</option>
    点赞 评论 复制链接分享
  • doufang8282 doufang8282 6年前

    Yii way to implement this functionality.

    You can keep the form state by setting the user input value to Model properties. For this, you can use CFormModel to implement, same like YII's default login page. Below is a sample example.

    1. Create a form model for your search (SearchForm.php) and place this inside models folder.

      class SearchForm extends CFormModel
              public $search_key;
              public $search_cat;
              public function rules()
                      return array(           
                              array('search_key,search_cat', 'required'),
    2. Assume i am using SiteController. I want to show this search form in my index page. When i submit the form it will submitted to search action

       class SiteController extends Controller
          public function actionIndex()
              $searchModel=new SearchForm();
          public function actionSearch()
                $searchModel=new SearchForm();


    $searchModel->attributes=$_POST['SearchForm']; That is i am resetting the user inputs to model.So, in your view the form will appear with user input values.

    1. Call this Form in views

          $form = $this->beginWidget('CActiveForm', array(
              'id' => 'search-form',
              'enableClientValidation' => true,
              'action'=>array('default/search'), //Submiting my form to Search action
                  <?php echo $form->textField($searchModel, 'search_key'); ?>    
                  $htmlOptions = array('size' => '1', 'prompt' => 'Select'); 
                  $list = array('0' => 'Men', '1' => 'Women'); // You can load your Categories from the Database table/Model.
                  echo $form->dropDownList($searchModel, 'search_cat', $list, $htmlOptions);
                  <?php echo CHtml::submitButton('Search'); ?>
          <?php $this->endWidget(); ?>

    Hope, This will help you for your better practice.

    点赞 评论 复制链接分享
  • douyingbei1458 douyingbei1458 6年前


    echo CHtml::dropDownList('cat',isset( $_REQUEST['cat'] ) ? $_REQUEST['cat'] : NULL, array('0'=>'Men', '1'=>'Women'),
    array('empty'=>'Select Category', 'class' => 'form-control selectpicker'));

    to achieve yii style,


    点赞 评论 复制链接分享
  • doujiku1028 doujiku1028 6年前

    You will have to pass the selected option via the controller back to the view.

    In the controller you will need something like this:

    $this->render('viewName', array('name' => 'valueOfTheList'))

    Then in the view you can use

    <option value = '0' <?php if($name == '0') echo "selected" ?>>Men</option>
    <option value = '1' <?php if($name == '1') echo "selected" ?>>Women</option>

    However. Since you are using Yii. I would advise you to look at CHTML::dropDownList(). Then you could do something like

    <?php echo CHtml::dropDownList('name', $select, 
              array('M' => 'Male', 'F' => 'Female'));

    Which is really a more Yii way to approach these kind of things.

    点赞 评论 复制链接分享