2017-08-07 3 views
1

このループを使用してHTML要素を取得できる次のjQueryコードがあります。私は自分のデータを取得し、それをAJAX経由でデータベースに保存したいと考えています。どうしたらいいですか?laravel/ajaxを使用して複数の値を取得してデータベースに格納する方法

私のコントローラで
$('#btnSubmit').on('click', function(){ 
    for(var increment = 0; increment <= pos_inc; increment++) { 
    var text1 = $('#orders' + increment).val(); 
    var text2 = $('#item_quan' + increment).val(); 
    var text3 = $('#price_tot' + increment).val(); 
    } 
}); 

HTML

<table class="table dynamic_field">       
    <label>OR Number:&nbsp;&nbsp;<span id="sp_or"><?php echo $or_no; ?></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
    <br> 
    <br> 
    Sold To:&nbsp;&nbsp;<span id="sp_name"><?php echo $name; ?></span></label> 
    <thead> 
    <th>Items</th> 
    <th>Qty</th> 
    <th>Total</th> 
    </thead> 
    <tbody> 
    {{ csrf_field() }} 
    <!-- empty --> 
    </tbody> 
    </table> 
    <!-- hidden input--> 
    <input type="hidden" id="inc" value="<?php echo $inc; ?>"> 
    <input type="hidden" id="or" name="or"> 
    <input type="hidden" id="cus_name" name="cus_name"> 
    <input type="hidden" id="total" name="total"> 
    <!-- end of hidden input--> 

    <label>TOTAL:&nbsp;&nbsp;&nbsp;<span id="sp_tot"><?php echo $total; ?></span></label> 
    <br> 
    <label>CUSTOMER CHANGE:&nbsp;&nbsp;&nbsp;<?php echo $change; ?></label> 
    <br> 
    <br> 
    <br> 
    <a href="{{ url('use-pos') }}"> 
    <button type="button" class="btn btn-default">Go Back</button> 
    </a>&nbsp; 
    <input type="button" class="btn btn-success" value="Complete Purchase" id="btnSubmit"> 
</form> 

:ここ

public function DoAddSales(Request $request){ 
    return $request->all(); 
} 
+1

はちょうど '$、それらはすべて同じ形にしていると仮定すると、あります( '#yourForm')。serialize() 'それを行うべきです。それがうまくいかない場合は、サーバーがデータを受け取ることを期待しているフォーマットと共に、HTMLやフルJSなど、より有用な情報を提供する必要があります –

+0

HTML +コントローラのフォームを表示してください関数 ! – Maraboc

+0

フォームのヘッダはありましたか? – Maraboc

答えて

0

はあなたのソリューションは、

$('#btnSubmit').on('click', function(){ 
    for(var increment = 0; increment <= pos_inc; increment++) 
    { 

     var text1 = $('#orders'+increment).val(); 
     var text2 = $('#item_quan'+increment).val(); 
     var text3 = $('#price_tot'+increment).val(); 

    } 

$.ajax({ 
     type: "POST", 
     contentType: "application/json; charset=utf-8", 
     url: "yoururlhere", 
     data: "{'text1':'" + text1+ "', 'text2':'" + text2+ "', 'text3':'" + text3+ "'}", 
     success: function (result) { 
      //do somthing here 
     } 
}); 

}); 
関連する問題