CI:如何获取从View传递到另一个View的数据

时间:2018-09-19 23:29:20

标签: codeigniter for-loop codeigniter-3 codeigniter-2

我能够在我的 FirstView.php 上循环获取数据。除获取的数据外,循环中还有$i值和单选按钮值。因此,当用户选择单选按钮并提交表单时,我希望将单选按钮的值和$i传递给 SecondView.php 以显示已选择的服务。但是,我只能在我的 SecondView.php 上显示的循环中获取最后一个值

FirstView.php

<?php echo validation_errors(); ?>
<?php echo form_open('Bookings/Booking'); ?>       
<table class='table table-hover'>
    <thead class='thead-light'>
        <tr>
            <th scope='col'></th>
            <th scope='col'>Options Without Possessions</th>
            <th scope='col'></th>
            <th scope='col'></th>
            <th scope='col'></th>
        </tr>
    </thead>
    <tbody>
        <?php
        $i = 1;
        foreach ($quotes_fetched as $row_quotes_fetched) { 
        echo "   
        <tr>
            <th scope='row'>$i <input type='hidden' name='test' id='test' value='".$i."'size='1'/></th>
            <td>".$row_quotes_fetched->ori."</td>
            <td>". $row_quotes_fetched->dest ."</td>
            <td>$". round($row_quotes_fetched->sellcost, 2)."</td>
            <td><input type='radio' name='Bk_Option' id='Bk_Option_0' class = 'Bk_OptionSel' value='$row_quotes_fetched->sellcost' required /></td>
        </tr>"; $i++;}?>
    <thead class='thead-light'>
        <tr>
            <th scope='col'></th>
            <th scope='col'>Options With Possessions</th>
            <th scope='col'></th>
            <th scope='col'></th>
            <th scope='col'></th>
        </tr>
    </thead>

控制器(Booking.php)

public function Booking(){
    $this->form_validation->set_rules('Bk_Option', 'Pleae select one option','required');
    $service = $this->input->post('Bk_Option');
    $this->session->set_userdata('service_selected', $service);
    $hiddentext = $this->input->post('test');
    $this->session->set_userdata('selTest', $hiddentext);
    $data['Cont'] = $this->session->userdata('selTest');      
    $qid  = $this->session->userdata('qid');
    $this->load->view('SecondView'. $data);
}

SecondView.php

<?php echo 'Service Selected: '.$Cont ?>

结果:

例如,在FirstView.php上获取了 4行,然后选择第二行以及 $i = 2 的值和< strong> radio button = 200 ,然后单击提交,它将显示单选按钮的正确值,但显示为 $i is 4 的值。

1 个答案:

答案 0 :(得分:0)

  1. 将data-id =“ $ i”添加到单选按钮
  2. 对单击单选输入进行JavaScript函数调用,并将带有该单选的data-id存储到隐藏输入(隐藏输入应在​​foreach循环之外) 3.jquery是必需的