【问题标题】:how to retrieve data from array of form fields如何从表单字段数组中检索数据
【发布时间】:2015-10-23 11:53:09
【问题描述】:

我想从表单字段数组中检索数据,我该怎么做

查看:

 <li class="checkbox"><input type="checkbox" value="yes" name="options[ac]" id="ac"> <label for="ac">Air Conditioning</label></li>
    <li class="checkbox"><input type="checkbox" value="yes" name="options[parking]" id="car"> <label for="car">Car Parking</label></li>
    <li class="checkbox"><input type="checkbox" value="yes" name="options[cards]" id="card"> <label for="card">Credit & Debit Card</label></li>
    <li class="checkbox"><input type="checkbox" value="yes" name="options[tv]" id="tv"> <label for="tv">TV</label></li>
    <li class="checkbox"><input type="checkbox" value="yes" name="options[wifi]" id="wifi"> <label for="wifi">Wi-Fi</label></li>
    <li class="checkbox"><input type="checkbox" value="yes" name="options[beverages]" id="beverage"> <label for="beverage">Beverages</labell>


<input name="option[adress]" class="form-control" type="text" 
placeholder="Address" required>

 <input name="option[email]" class="form-control" type="text"     placeholder="email" required>

我想将每个名称和值显示为: 例如地址:abcd wifi:是的电子邮件:abcd

我如何分配我尝试过的关联数组选项

    $arr=$this->input->post('option[]');

    foreach($arr as $key => $value) 
    {
        echo($key);
    }

【问题讨论】:

  • 帮自己一个忙,只需在接收表单提交的 PHP 页面中放置 print_r($_POST)var_dump($_POST)。填写您的表格,提交并仔细查看打印到屏幕上的数据。熟悉表单数据是如何发布到脚本的,包括传递的内容和不传递的内容。
  • 粘贴您的完整表单,以便我们知道您实际上在使用发布请求。

标签: php forms codeigniter associative-array


【解决方案1】:

$this->input-post('field_name') 在 $_POST 数组中查找名为 'field_name' 的键。试试这个。

$arr=$this->input->post('option[adress]');

foreach($arr as $key => $value) 
{
    echo($key);
}

【讨论】:

  • 明智的做法是始终检查来自input_post('field_name') 的返回,因为如果'field_name' 不在$_POST 数组中,它将返回NULL。
【解决方案2】:

这样试试

$arr=$this->input->post('option');

【讨论】:

  • 没有错误出现:为 foreach() 提供的参数无效
【解决方案3】:

试试这个

<?php
$arr = $_POST["option"];

foreach($arr as $key => $value){
   echo $key.":".$value;
}
?>

【讨论】:

    猜你喜欢
    • 2021-09-22
    • 2011-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-21
    • 1970-01-01
    相关资源
    最近更新 更多