codeigniter - 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
.
解决方案
- 将 data-id="$i" 添加到单选按钮
- 在单击单选输入上调用 JavaScript 函数并将带有该单选的数据 ID 存储到隐藏输入(隐藏输入应在 foreach 循环之外) 3.jquery 是必需的
推荐阅读
- bash - 如何使用选项远程执行 bash 脚本?
- javascript - Highcharts xrange 拒绝遵循 HOVER 或 SCROLLBAR 行为
- ios - iphone & ipad textarea 字体不呈现某些字符
- reactjs - React 简单的全局实体缓存而不是 Flux/React/etc
- mysql - mysql - 如何强制更改内部连接的评估顺序?
- r - purrr 替换拆分、应用、输出嵌套列
- c# - WPF C# Binding OnTargetPropetyChanged 在后面的代码中
- r - 如果后跟 R 中的某个值,则删除值
- javascript - 使用纯 Javascript 将类添加到特定元素
- mysql - SQL查询复制excel的INDEX/MATCH功能