【问题标题】:laravel 4 form submit with multiple inputlaravel 4表单提交多个输入
【发布时间】:2013-06-28 05:44:12
【问题描述】:

这是 var_dump($_POST) :

array(18) { ["_token"]=> string(40) "Qg0krYddkI2cnPQBy5T3yGJdQqRBbb9q173MXzoa" ["from_name"]=> string(2) "4r" ["from_address"]=> string(1) "4" ["invoice_id"]=> string(1) "4" ["invoice_date"]=> string(0) "" ["due_date"]=> string(0) "" ["to_name"]=> string(1) "4" ["to_address"]=> string(1) "4" ["item"]=> array(1) { [0]=> string(5) "Hours" } ["desc"]=> array(1) { [0]=> string(2) "44" } ["​unitAmt"]=> array(1) { [0]=> string(1) "4" } ["​qty"]=> array(1) { [0]=> string(1) "4" } ["​amount"]=> array(1) { [0]=> string(2) "16" } ["invoiceNotes"]=> string(2) "44" ["subTotal"]=> string(2) "16" ["total"]=> string(2) "16" ["amtPaid"]=> string(1) "0" ["balDue"]=> string(2) "16" }

正如您所看到的,变量 unitAmt 正在发布,但是当我使用它时出现此错误:

ErrorException
Undefined index: unitAmt
open: /var/www/lk/htdocs/app/routes.php
//var_dump($rows);
//var_dump($description);


for($i=0; $i<count($rows);$i++){
    DB::table('item_description')->insert(
    array('invoice_id' => $returnID, 'item' => $rows[$i], 'description' => $description[$i],
    'unit_price' => $_POST['unitAmt'][$i], 'quantity' => $_POST['​qty'][$i], 'amount'=>$_POST['​amount'][$i]));
     }

这适用于类似发布的数量和金额。同样的事情在其他地方也发生在转储变量时我可以看到数据在那里但是当我使用它时显示未定义的索引。

编辑: 这是我在 route.php 中的代码

    var_dump($_POST);

$rows = $_POST['item'];
$description = $_POST['desc'];


 for($i=0; $i<count($rows);$i++){
    DB::table('item_description')->insert(
    array('invoice_id' => $returnID, 'item' => $rows[$i], 'description' => $description[$i],
    'unit_price' => $_POST['unitAmt'][$i], 'quantity' => $_POST['​qty'][$i], 'amount'=>$_POST['​amount'][$i]));
     }

【问题讨论】:

  • 检查这三个字段,你的 var_dump 显示?在键名中,因此它与 $_post['unitAmt'].....["?unitAmt"]=> array(1) { [0]=> string(1) "4" } [" 不匹配?qty"]=> array(1) { [0]=> string(1) "4" } ["?amount"]=> array(1) { [0]=> string(2) "16" }
  • 没看懂你写的内容。
  • 检查为什么“问号”出现在您的 var_dump 中的 unitAmt、qty 和 amount 字段之前。由于“?unitAmt”数组键,您得到的错误未定义索引unitAmt。
  • 我看不到 ["​unitAmt"]=> array(1) { [0]=> string(1) "4" } 中的问号在哪里
  • 如果需要“问号”,则将其添加到您的 $_POST['unitAmt'][$i] 中,即 $_POST['?unitAmt'][$i]

标签: php forms laravel laravel-4


【解决方案1】:

我建议您使用 Eloquent 模型以这种方式插入 ITEM 数据,并循环遍历结果集中的所有项目:-

使用输入类以laravel方式获取POST和GET变量

 Input::get('invoice_id') 

method get() - 返回 POST 和 GET 变量 method e() - 将 HTML 字符转换为实体并在 laravel/helper.php 中定义

使用 Eloquent 以 laravel 方式在 db 中添加新行,例如:-

item_description::create($arr);

以更简洁的 Laravel 方式将项目添加到 item_description 表的简单示例:-

$arr = array(
    'invoice_id' => e(Input::get('invoice_id')),
    'item' => e(Input::get('item')),
    'description' => e(Input::get('desc')),
    'unit_price' => e(Input::get('unitAmt')),
    'quantity' => e(Input::get('qty')),
    'amount' => e(Input::get('amount')),
    );   

    // Insert Data in table
    $item_description= item_description::create($arr);

【讨论】:

    【解决方案2】:

    你为什么使用 Laravel 来使用标准的 PHP 函数?这种插入代码不应该在routes.php 文件中,它应该在控制器或闭包中。您可能应该使用Eloquent 模型来创建项目。此外,您可以使用Input 类来检索由 GET 或 POST 参数提供的数据。

    【讨论】:

      猜你喜欢
      • 2014-08-05
      • 2014-10-12
      • 2013-06-18
      • 2019-11-04
      • 1970-01-01
      • 2019-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多