【问题标题】:how to post html field arrays in forms using php如何使用 php 在表单中发布 html 字段数组
【发布时间】:2015-07-13 19:34:09
【问题描述】:

我有一个包含 2 个字段的表单。

 <td class="left"><input style="  width: 30%; text-align: center;" type="text" name="name[]" value="<?php echo $geo_zone['name']; ?>" /></td>
 <td class="left"><input style="  width: 30%; text-align: center;" type="email" name="email[]" value="<?php echo $geo_zone['email']; ?>" /></td>

如果我 print_r 我的数组,我会得到完美的结果。像这样

$name = $this->request->post['name'];;
$email = $this->request->post['email'];; 

但我想要的是继续在数据库中插入这两个值,直到数组为空($name 和 $email)。为此,我尝试了每个循环,但它只在数据库中插入一行。

 foreach( $name as $key => $n ) {

    $this->data['geo_zone_id']=$this->model_module_shipping_pools->Get_geo_zone_id($n);


    $geo_zone_id=$this->data['geo_zone_id'];
    $geo_zone_id=$geo_zone_id[0]['geo_zone_id'];

    $this->model_module_shipping_pools->drop_data();
    $email=$email[$key];

    $this->model_module_shipping_pools->insertData($geo_zone_id,$email);

    }

【问题讨论】:

  • but what i want is to keep inserting these two values in database until arrays are empty ($name and $email).你能详细解释一下吗?
  • @anantkumarsingh 实际上我的表单是根据数据库中的行动态创建的,因此这两个电子邮件和姓名数组可能包含多个值。你在 html 中只看到两个输入字段,但它们是在运行时生成的,我使用 name[] 和 email[] 来存储多个值
  • @anantkumarsingh 知道为什么我的循环只运行一次

标签: php sql model-view-controller opencart


【解决方案1】:

$email=$email[$key]; - 您正在覆盖您的 $email 变量。叫它别的吧。

【讨论】:

  • 将其更改为 $mail ,然后传递给模型函数仍然是一行。最后一行总是被插入
  • 知道为什么我的循环只运行一次
  • 你能 var_dump $name 和 $email 吗?
  • 是的,我得到了这个结果.. array(2) { [0]=> string(5) "Texas" [1]=> string(7) "Florida" } array(2) { [0]=> 字符串(13)“abs@gmail.com”[1]=> 字符串(13)“xyz@gmail.com”}
猜你喜欢
  • 2012-01-27
  • 2011-06-10
  • 1970-01-01
  • 1970-01-01
  • 2014-08-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多