1

i have a code that stores my data in a database. but everytime it stores in the database, it only gets the last data and not the whole data from my increment.

 $('#btnSubmit').on('click', function(){
    for(var increment = 0; increment <= pos_inc; increment++){
        var text1 = $('#id_item'+increment).val();
        var text2 = parseInt($('#item_quan'+increment).val()); 
        var text3 = $('#price_tot'+increment).val();
        var text4 = $('#or').val();
        var text5 = $('#cus_name').val();
        var text6 = $('#total').val();
        var text7 = parseInt([$('#id_categ'+increment).val()]);

        $.post('DoAddSales', {
            '_token': $('input[name=_token]').val(),
            'orders': text1,
            'item_quan': text2, 
            'price_tot': text3,
            'or': text4,
            'cus_name': text5,
            'total': text6,
            'id_categ': text7,
         },function(data){
            window.location.href = "use-pos";
         });
    }
});

in my controller i have the following:

$inc = $request->input('inc');   
$inc2 = 1;
for($inc2; $inc2 <= $inc; $inc2++){
    $ix = ($request->input('i_name'.$inc2));
    $ix2 = ($request->input('quan'.$inc2));  
    $ix3 = ($request->input('tot'.$inc2));
    $ix4 = ($request->input('id_categ'.$inc2));
    $ix5 = ($request->input('id_item'.$inc2));

    echo '<input type="hidden" name="orders'.$inc2.'" id="orders'.$inc2.'" class="getOrders" value="'.$ix.'" disabled>'; 
    echo '<input type="hidden" name="item_quan'.$inc2.'" id="item_quan'.$inc2.'" class="getOrders" value="'.$ix2.'" disabled>';
    echo '<input type="hidden" name="price_tot'.$inc2.'" id="price_tot'.$inc2.'" class="getOrders" value="'.$ix3.'" disabled>';
    echo '<input type="hidden" name="id_categ'.$inc2.'" id="id_categ'.$inc2.'" class="getOrders" value="'.$ix4.'" disabled>';
    echo '<input type="hidden" name="id_item'.$inc2.'" id="id_item'.$inc2.'" class="getOrders" value="'.$ix5.'" disabled>';                   
}  
echo '<input type="hidden" id="inc" value="'.$inc.'" disabled>';
return view('show_sales')->with(
    array('name'=>$request->input('customer_name'),
        'or_no'=>$request->input('or_number'),          
        'total'=>$request->input('samp'),            
        'change'=>$request->input('change'),
        'inc'=>$inc,
    ));

i am still new to AJAX and Laravel, and is still learning, how can i output all the data with the increments from the inputs and save it in the database?

1
  • it only gets the last value "btnSubmit" increment Commented Aug 9, 2017 at 5:49

3 Answers 3

1

Use:

print_r(Input::all()); 

To check how many rows are passing to the controller for saving.

Sign up to request clarification or add additional context in comments.

2 Comments

to check weather problem is in Jquery or in saving controller function
@JYoThI please don't format each word as code, it makes it quite difficult to read. If you want to show emphasis then use the appropriate formatting. Thank you.
0

You can use the Input::all() function to check rows are passing to the controller.

Comments

0

It is because your using $.post method in jquery which is asynchronous in your case your loop will to continue even though your ajax request is not yet done so try to use

$.ajax so that you can set the behavior of your ajax and set the async: false so that your ajax request runs in synchronous

Let me know if this helps you. Good Luck

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.