简体   繁体   中英

laravel 4 form submit with multiple input

this is the 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" }

As you can see the variable unitAmt is being posted, but I am getting this error when I use it :

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]));
     }

This works fine for qty and amount which are posted similarly. Same thing is happening at other places also on dumping a variable I can see data is there but when I use it shows undefined index.

Edit : THis is my code in 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]));
     }

Why are you using Laravel to use standard PHP functions? That kind of insertion code shouldn't be in the routes.php file, it should be in a controller or a closure. You should probably using an Eloquent model to create items. Furthermore, you can use the Input class to retrieve data that is provided by GET or POST parameters.

I recommend you to use Eloquent model to insert an ITEM data like this way and also loop thru for all the items in your result set:-

Use input class to get POST and GET variables in laravel way

 Input::get('invoice_id') 

method get() - returns the POST and GET vars method e() - Convert HTML characters to entities and defined in laravel/helper.php

Use Eloquent to add new row in db in laravel way like:-

item_description::create($arr);

A simple example that is adding an item to item_description table in a cleaner Laravel way:-

$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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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