如何在codeigniter中搜索后保留选定的框选项

问题描述 投票:1回答:3

如何在search.i想要在我选择所选选项并搜索它时打印所选的框选项。搜索之后我应该搜索我选择的内容。

this is my view page

 <?=  form_open("../Admin/getRecords");    ?> 
        <div class="" style="height:5.5vw;border-bottom:1.2px solid lightgrey;padding:.5%;">
        <div class="col col-lg-3" style="margin-left:41%;margin-top:0%;padding-bottom: 2%;">
            <label>ITR Status</label> <select name="ITRStatus" id="ITRStatus" class="form-control chosen" style="height:2.5vw;" id="sel">
                 <?php 
           // $a = $_POST['ITRStatus'];
           
            $name = set_value('ITRStatus');
//            echo '<pre>';
//            print_r($name);
            
            if(count($alldata)): 
                         foreach($alldata as $data):
                
         ?>
                <option  value="<?php echo $data->id; ?><?php $name == $data->id ? "selected":""?>
  ?>"><?php echo $data->itr_status ?></option>
<!--                <option value="<?php //echo $record->id; ?>"><?php //echo $record->assessment_year ?>2019-20</option>-->
                <?php  endforeach;endif;    ?>
            </select>
      </div> 

这是我的控制器页面

public function getRecords()
{
  $this->load->model('AdminModel');
  $year = $this->input->post('year');
  $ITRStatus = $this->input->post('ITRStatus');



 $alldata = $this->AdminModel->getItrStatus(); 

 $assessments = $this->AdminModel->getAssessmentModel();   

$records = $this->AdminModel->getRecords($year,$ITRStatus); 
  if($records == 1 )

    {
         $this->itrDetail();

    }


 $this->load->view('SearchItr',['records' => $records,'alldata' => $alldata,'assessments' => $assessments]);   

}
php codeigniter
3个回答
0
投票

你的逻辑是正确的,但你错误的选项标签。

<option 
    value="<?php echo $data->id ?>"
    <?php $name == $data->id ? "selected":"" ?>
>

    <?php echo $data->itr_status ?>

</option>

0
投票

好吧,我给你举例说明如何做到这一点

视图

<!doctype html>
<html>
 <body>
   <div>
   <?php
   if(isset($response)){
      echo "<b>Name :</b> ".$response['txt_name']."<br/>";
      echo "<b>Username :</b> ".$response['txt_uname']."<br/>";
      echo "<b>Email :</b> ".$response['txt_email']."<br/>";
   }
   ?>
   </div>

   <form method='post' action='<?php echo base_url(); ?>'>

     <table>
       <tr>
          <td>Name</td>
          <td><input type='text' name='txt_name'></td>
       </tr>
       <tr>
          <td>Username</td>
          <td><input type='text' name='txt_uname'></td>
       </tr>
       <tr>
          <td>Email</td>
          <td><input type='text' name='txt_email'></td>
       </tr>
       <tr>
          <td>&nbsp;</td>
          <td><input type='submit' name='submit' value='Submit'></td>
       </tr>
     </table>

   </form>
 </body>
</html>

调节器

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class User extends CI_Controller {

 public function __construct() {

   parent::__construct();

   // load base_url
   $this->load->helper('url');
 }

 public function index(){
   $data = array();
   // Check form submit or not
   if($this->input->post('submit') != NULL ){

     // POST data
     $postData = $this->input->post();

     $data['response'] = $postData;

   }

   // load view
   $this->load->view('user_view',$data);

 }

}

注意:您必须将所需数据传递给视图,就像您想要选择的值一样,这样也必须通过才能进入视图


0
投票

更改您的视图页面代码

<?=  form_open("../Admin/getRecords");    ?> 
    <div class="" style="height:5.5vw;border-bottom:1.2px solid lightgrey;padding:.5%;">
    <div class="col col-lg-3" style="margin-left:41%;margin-top:0%;padding-bottom: 2%;">
        <label>ITR Status</label> 
        <select name="ITRStatus" id="ITRStatus" class="form-control chosen" style="height:2.5vw;" id="sel">
             <?php            
             $name = set_value('ITRStatus');

            if(count($alldata)): 
               foreach($alldata as $data): ?>
                <option  value="<?php echo $data->id; ?>" <?php if($name == $data->id){ echo "selected"; } ?>><?php echo $data->itr_status; ?></option>
            <?php  endforeach;endif;    ?>
        </select>
  </div> 
© www.soinside.com 2019 - 2024. All rights reserved.