0

I'm very new in web programming, especially on Codeigniter. And now I'm looking for how to pass/submit array from view to controller.

This part of my HTML script in view:

<tr class="rowdim"> <!-- ROW 1 -->
<td><input type="text"  id="bookid1" name="book_id[]" /></td>
<td><input type="text"  id="qty1" name="qty[]" /></td>
<td><input type="text"  id="uom1" name="uom_id[]" /></td>
</tr>

<tr class="rowdim"> <!-- ROW 2 -->
<td><input type="text"  id="bookid2" name="book_id[]" /></td>
<td><input type="text"  id="qty2" name="qty[]" /></td>
<td><input type="text"  id="uom2" name="uom_id[]" /></td>
</tr>

<tr class="rowdim"> <!-- ROW 3 -->
<td><input type="text"  id="bookid3" name="book_id[]" /></td>
<td><input type="text"  id="qty3" name="qty[]" /></td>
<td><input type="text"  id="uom3" name="uom_id[]" /></td>
</tr>

My ajax:

var det_book = document.getElementsByName("book_id[]");
var det_qty = document.getElementsByName("qty[]");
var det_uom = document.getElementsByName("uom_id[]");
var vdata = {det_book:det_book,det_qty:det_qty,det_uom:det_uom}
$.ajax({
    type:"POST",
    url:"<?php echo base_url(); ?>trans/StockIn/saveData",
    data:vdata,
    success:function(returnmsg){
        if (returnmsg=='""'){
             window.alert(msg);
         } else {
             window.alert(returnmsg);
         }
});

Controller:

 $det_book=$_POST["det_book"];
 $det_qty=$_POST["det_qty"];
 $det_uom=$_POST["det_uom"];
 $details = array();
 $index=0;
 foreach ($det_book as $baris){
 array_push($details,array(
    'book_id'=>$baris,
    'quantity'=>$det_qty[$index],
    'uom_id'=>$det_uom[$index]
));
$index++; }
$error="";
if (!$this->db->insert_batch('trx_inbound_detail',$details))
{
    $error = $this->db->error();
}

Any miss or something wrong with my code? Already search in community but still no luck. Appreciate if you also suggest other ways. Thanks

HooHoo
  • 3
  • 2
  • Replace `var det_uom = document.getElementsByName("uom_id[]");` with `var det_uom = document.getElementsByName("uom[]");` – Twinkle Nov 21 '18 at 03:48
  • Edited. Passing done, controller still got issue. A PHP Error was encountered Severity: Warning Message: Invalid argument supplied for foreach() Filename: trans/StockIn.php Line Number: 98 Backtrace: File: D:\xampp\htdocs\penerbit\application\controllers\trans\StockIn.php Line: 98 Function: _error_handler – HooHoo Nov 21 '18 at 03:58
  • Did you check with the browser's dev tools or the console? – gre_gor Nov 21 '18 at 17:09

2 Answers2

0

Yes, you missed something. Element with name book_id[] doesn't exist. Also you have three inputs with same name. Check this link to see how to pass array with ajax.

Pararera
  • 363
  • 3
  • 15
  • edited, but looked like the error comes from controller, in **foreach** part – HooHoo Nov 21 '18 at 03:57
  • Check for errors from JS side, if JS is okey then problem is with PHP. – Pararera Nov 21 '18 at 04:08
  • Just found the problem is in this part: `var det_book = document.getElementsByName("book_id[]")` capturing array data in JS from the input name. because when i try hardcode the array, everything's going fine. Any idea? – HooHoo Nov 22 '18 at 02:51
  • @HooHoo Remove `[]` from `name` tag. Btw. what console(in browser) says – Pararera Nov 22 '18 at 03:09
  • It says `Uncaught RangeError: Maximum call stack size exceeded at Object.toString ()` Still same error even I remove the bracket `[]` from name tag – HooHoo Nov 22 '18 at 08:02
0
Your first mistake is get the textbox value in multiple fields:

    var det_book = $('input[name^=book_id]').map(function(idx, elem) {
        return $(elem).val();
    }).get();

    var det_qty = $('input[name^=qty]').map(function(idx, elem) {
        return $(elem).val();
    }).get();

    var det_uom = $('input[name^=uom_id]').map(function(idx, elem) {
        return $(elem).val();
    }).get();

In php you didnot mention the index in foreach:

    foreach ($det_book as $index => $baris) {
         array_push($details,array(
            'book_id'=>$baris,
            'quantity'=>$det_qty[$index],
            'uom_id'=>$det_uom[$index]
        ));
    }

    print_r($details);
    exit();
AngularJMK
  • 1,178
  • 13
  • 15
  • 1
    Yes, actually I don't understand to get the `textbox` in multiple fields. Using your method, it's work. Thanks a lot for your help :) – HooHoo Nov 22 '18 at 10:27