简体   繁体   English

CodeIgniter:从表单数组获取数据到post数组

[英]CodeIgniter: Getting data from form array into post array

I'm racking my brain. 我在绞尽脑汁。 I'm using CodeIgniter, trying to get a value from a form checkbox into a data array to send off to the database. 我正在使用CodeIgniter,尝试从表单复选框获取值到数据数组以发送到数据库。 Here are the snippets: 以下是片段:

Form (view): 表格(查看):

<label>Rental Car?</label><input type="checkbox" name="options[is_rental]" value="1" <?php echo set_checkbox('options[is_rental]', '1', FALSE); ?> />

Controller: 控制器:

$data['is_rental'] = $this->input->post('options[is_rental]');

Now, during this process I'm also validating and re-populating the form with data using options[is_rental] and that works just fine. 现在, 在此过程中,我还使用选项[is_rental]验证并使用数据重新填充表单,并且工作正常。 Using var_dump I get: 使用var_dump我得到:

Dumps (with the checkbox checked) from the controller: 从控制器转储(选中复选框):

var_dump($this->input->post('options[is_rental]'))

Returns 返回

bool(false)

and... 和...

var_dump($this->input->post('options'))

Returns 返回

array(3) { ["engine"]=> string(4) "4cyl" ["transmission"]=> string(9) "automatic" ["is_rental"]=> string(1) "1" }

For what it's worth, I can't get to those other values in the array either. 对于它的价值,我也无法获得数组中的其他值。

Why not just do: 为什么不这样做:

$data = $this->input->post('options'); $ data = $ this-> input-> post('options');

Then $data['is_rental'] should == 1 那么$ data ['is_rental']应该== 1

I've noticed the same thing with CodeIgniter. 我已经注意到了CodeIgniter的相同之处。 If you pass the index to the form validation rule, it works fine, but to get the data into another var, you need to first put the post array into a temp variable and then access that index. 如果将索引传递给表单验证规则,它可以正常工作,但要将数据放入另一个var,您需要先将post数组放入临时变量,然后访问该索引。 You cannot access indexes on POST array's by using $this->input->post 您无法使用$ this-> input-> post访问POST阵列上的索引

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM