在单击提交按钮后,如何将逐行数据提交给laravel 5.2中的控制器?

时间:2017-06-12 05:09:21

标签: javascript jquery ajax laravel-5.2

在ajax的帮助下加入两个表后我带了一些数据,现在我需要在点击提交按钮后逐行提交这些数据。

Serial_no|member_id|member_name|account_no|General_saving|Dps|actions
 1       | 1111111 | zia       | 01010101 |100           |190|submit


 2       | 2222222 |shishir    | 02020202 |100           |200|submit


 3       | 3333333 | shohan    | 03030303 |100           |230|submit

我想点击提交按钮将行数据发送到控制器到数据库。我的HTML代码在下面是blade:

<div class="col-md-12" style="background-color: #EAEAEA">
<div class="form-group col-md-12" id="form-MemberPresentAddress-error">
    <h3>Account Information</h3>               
</div>
<table class="table table-bordered table-striped">
<thead>
<tr>
    <th width="50px" style="text-align: center">No</th>
    <th>
        <a href="javascript:ajaxLoad('posting/list?field=MemberId&sort=
{{Session::get("posting_sort")=="asc"?"desc":"asc"}}')">
            Member ID
        </a>
        <i style="font-size: 12px"
           class="glyphicon  {{ Session::get('posting_field')=='MemberId'?
(Session::get('posting_sort')=='asc'?'glyphicon-sort-by-
alphabet':'glyphicon-sort-by-alphabet-alt'):'' }}">
        </i>
    </th>
    <th>
        <a href="javascript:ajaxLoad('posting/list?field=MemberName&sort=
{{Session::get("posting_sort")=="asc"?"desc":"asc"}}')">
            Member Name
        </a>
        <i style="font-size: 12px"
           class="glyphicon  {{ Session::get('posting_field')=='MemberName'?
(Session::get('posting_sort')=='asc'?'glyphicon-sort-by-
alphabet':'glyphicon-sort-by-alphabet-alt'):'' }}">
        </i>
    </th>
     <th>
        <a href="javascript:ajaxLoad('posting/list?field=AccountNo&sort=
{{Session::get("posting_sort")=="asc"?"desc":"asc"}}')">
            Account No
        </a>
        <i style="font-size: 12px"
           class="glyphicon  {{ Session::get('posting_field')=='AccountNo'?
(Session::get('posting_sort')=='asc'?'glyphicon-sort-by-
alphabet':'glyphicon-sort-by-alphabet-alt'):'' }}">
        </i>
    </th>
    <th>
        <a href="javascript:ajaxLoad('posting/list?field=app_form&sort=
{{Session::get("posting_sort")=="asc"?"desc":"asc"}}')">
            General Saving
        </a>
        <i style="font-size: 12px"
           class="glyphicon  {{ Session::get('posting_field')=='app_form'?
(Session::get('posting_sort')=='asc'?'glyphicon-sort-by-
alphabet':'glyphicon-sort-by-alphabet-alt'):'' }}">
        </i>
    </th>
    <th>
        <a href="javascript:ajaxLoad('posting/list?field=passbook&sort=
{{Session::get("posting_sort")=="asc"?"desc":"asc"}}')">
            DPS 
        </a>
        <i style="font-size: 12px"
           class="glyphicon  {{ Session::get('posting_field')=='passbook'?
(Session::get('posting_sort')=='asc'?'glyphicon-sort-by-
alphabet':'glyphicon-sort-by-alphabet-alt'):'' }}">
        </i>
    </th>

    <th width="140px">Actions</th>
</tr>
</thead>
<tbody id="searchShow" class="searchShow">

</tbody>
</table>
<div class="pull-right"></div>
<div class="row" id="totalRecord">

    Total: records

</div>
</div>

我的jquery代码如下:

$(document).ready(function () {
        $(document).on('click', '#searchInfo', function () {
            var DomainName = document.getElementById('DomainName').value;
            var DivisionOfficeId = 
                          document.getElementById('DivisionOfficeId').value;
            var ZoneId = document.getElementById('ZoneId').value;
            var AreaId = document.getElementById('AreaId').value;
            var MonthId = document.getElementById('MonthId').value;
            var YearId = document.getElementById('YearId').value;

            var i = 1;
            var j = 0;

            $('#searchShow').empty();
            $.ajax({
                type: 'get',
                url: 'getSearchinfo',
                data: {'DomainName': DomainName, 'DivisionOfficeId': 
 DivisionOfficeId, 'ZoneId': ZoneId, 'AreaId':AreaId, 'MonthId': MonthId, 
 'YearId': YearId},
                success: function (data) {

                    $.each(data, function (index, subcatObj3p) {
                        $('#searchShow').append('<tr><td style="text-align: 
center" id="'+i+'">' + i + '</td><td style="text-align: center">' + 
subcatObj3p.MemberId + '</td><td style="text-align: center">' + 
subcatObj3p.MemberName + '</td><td style="text-align: center">' + 
subcatObj3p.AccountNo + '</td><td><input style="width:30%" type="text" 
Name="Dps" value="100"></input></td><td><input style="width:30%" type="text" 
Name="Dps" value="'+subcatObj3p.MonthlyInstallment+'"></input></td><td>
<input type="submit" value="submit" id="goDB"></td></tr>');
                        i++;
                        j=i;


                    });
                    // document.getElementById('hidden').value = i - 1;
                    alert(j);
                    $('#totalRecord').val(j);
                },
                error: function () {
                }
            });

        });
    });

这是我的控制器功能:

 public function getSearchinfo(Request $request){

    $DomainName = $request->DomainName;
    $DivisionOfficeId = $request->DivisionOfficeId;
    $ZoneId = $request->ZoneId;
    $AreaId = $request->AreaId;
    $MonthId = $request->MonthId;
    $YearId = $request->YearId;

    $searchInfo = Accountopen::select('*')
                        ->join('members', 'accountopens.MemberId' , '=', 
 'members.MemberId')
                        ->where('members.DomainName', $DomainName)
                        ->orWhere('accountopens.DivisionOfficeId','=', 
 $DivisionOfficeId)
                        ->orWhere('accountopens.ZoneId','=', $ZoneId)
                        ->orWhere('accountopens.AreaId','=', $AreaId)
                        // ->where('accountopens.MonthId', $MonthId)
                        // ->where('accountopens.YearId', $YearId)
                        ->get();

    return response()->json($searchInfo);
}

现在我想点击提交按钮并逐个发送行数据。请回复我。我正在使用laravel 5.2版本。

2 个答案:

答案 0 :(得分:1)

为所有arr=Array(1,2,3,4,5) 'this array contains all your Total_1,2,3,4,5 values. You can add more. fullSum = func_sum(arr,UBound(arr))(1) 'In this case, the value is 15(sum of all elements) For i=0 To UBound(arr) sum = func_sum(arr,i) For k=1 To fullSum If k>sum(0) And k<=sum(1) Then WScript.Echo "Currently in Set "&i+1 End If Next Next Function func_sum(intArr, tempPos) 'returns the sums of all elements upto indices tempPos-1 and tempPos tempPos2=tempPos-1 sum1=0 sum2=0 If tempPos=0 Then sum2=intArr(tempPos) Else For j=0 To tempPos2 sum1 = sum1 + arr(j) Next sum2 = sum1 + arr(tempPos) End If arrSum = Array(sum1,sum2) func_sum=arrSum End Function class提供一个公共Update / Edit button,并传递anchor值以更新哪一行。并参考以下示例:

&#13;
&#13;
ID
&#13;
$('.edit').click(function(){
	alert( $(this).closest('td').attr('id') );
  // make ajax call here
});
&#13;
&#13;
&#13;

使用上面给出的方法并相应地进行ajax调用。

答案 1 :(得分:1)

$('#searchShow').append('<tr id="row"'+i+'><td style="text-align: 
center" id="'+i+'">' + i + '</td><td style="text-align: center" class="memberId">' + 
subcatObj3p.MemberId + '</td><td style="text-align: center" class="membeName">' + 
subcatObj3p.MemberName + '</td><td style="text-align: center" class="memberAccNum">' + 
subcatObj3p.AccountNo + '</td><td><input style="width:30%" type="text" 
Name="Dps" value="100"></input></td><td><input style="width:30%" type="text" 
Name="Dps" value="'+subcatObj3p.MonthlyInstallment+'"></input></td><td>
<input type="submit" value="submit" id="goDB" attr"'+i+'"></td></tr>');

$(document).on('click', '#goDB', function () {
    var attr = $this.attr();
    var parentId = "#"+row+attr;
    var memberId = $(parentId).child("#memberId").val();
    var memberName = $(parentId).child("#membeName").val();
    var memberAccNum = $(parentId).child("#memberAccNum").val();

    Then here you send post request to save these values to DB
});

您已使每一行都具有唯一ID以获取特定行数据,因此只需创建具有唯一ID的动态行并获取其子数据并作为发布请求传递。