首页 > 解决方案 > CI:如何获取从 View 传递到另一个 View 的数据

问题描述

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

第一视图.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);
}

第二视图.php

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

结果:

例如,在 FirstView.php 上提取了四行,我选择了第二行以及 中的值$i = 2值,radio button = 200然后单击提交,它会显示单选按钮的正确值,但$i is 4.

标签: codeigniterfor-loopcodeigniter-3codeigniter-2

解决方案


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

推荐阅读