【问题标题】:CI: How can I get the data passed from View to the other ViewCI:如何获取从 View 传递到另一个 View 的数据
【发布时间】:2018-09-19 23:29:20
【问题描述】:

我能够在我的 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中获取了四行,我选择了第二行和$i = 2的值和中的值radio button = 200 并点击提交,它会显示单选按钮的正确值,但 $i is 4 的值。

【问题讨论】:

  • 嗯 $i 变量仅对该视图是本地的。您似乎出于某种原因需要此值,因为它本质上是num_rows() + 1,您可以将其保存到控制器中的会话变量中以获取第一个视图。或将其设为第一个视图表单中的隐藏输入。
  • @Alex 我将它保存为会话并能够在第二视图中获取值,但是我从 $i 获得的值不是正确的值。它给出了 firstview.php 中循环中最后一个结果的值
  • 这是因为每次 foreach 迭代都会覆盖隐藏的输入;每个元素的名称和 ID 必须是唯一的。否则你只会得到名称为test 的最后一个元素。即使每次迭代都使元素名称唯一,也没有简单的方法。
  • 那我怎么才能让它独一无二呢?由于记录在循环中获取,有什么办法吗?
  • 使用 ajax 这将相当简单,因为您只需添加一个 data- 属性,因此,基本上,为 1 个输入提交 2 个值。但是,对于传统形式,您必须以某种方式将隐藏的输入值与无线电值相关联(想不出一种简单的方法来做到这一点)。在第二个控制器中进行查询以使用无线电值获取位置并根据需要添加 1 $i = 1 通常为 0 会更容易。您在运行 jquery 吗?

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


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

【讨论】:

    猜你喜欢
    • 2019-06-15
    • 1970-01-01
    • 2020-11-13
    • 2020-02-17
    • 2019-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多