通过弹出表单更新数据库时,表单提交不起作用

时间:2019-06-10 11:28:17

标签: javascript ajax laravel

表单提交不起作用。当我单击弹出窗口中的“更新”按钮时,页面将重新加载,并且数据未更新

这是脚本 $(document).off('click','。updatepro');  $(document).on('click','。updatepro',function(){

var userid = $(this).data('userid');
var url = baseurl+'/update';
var infoData= {userid:userid,_token:token};
$.post(url,infoData,function(response){

})

});

这是我的控制器 公共功能更新(请求$ request,$ id)     {

    $user = User:: findorFail($id);

    $user->name = $request->input('name');
    $user->username = $request->input('username');
    $user->email = $request->input('email');
    $user->address = $request->input('address');
    $user->phone = $request->input('phone');
    $user->designation = $request->input('designation');
    $user->deviceid = $request->input('deviceid');
    echo'<pre>';
    print_r($request-all());
    exit;

    $user->save();

}

这是路线 路线:: post('/ update','GetdataController @ update');

我想要的是要提交的数据,并从我选择要更新的数据的同一页面打开

1 个答案:

答案 0 :(得分:1)

您应该尝试以下操作:

//modal form to show all the required inputs and make one field hidden

<div id="updateFormID" class="modal fade" >
        <div class="modal-dialog box box-default" role="document">
          <div class="modal-content">
            <div class="modal-header">
              <h4 class="modal-title">Edit Bank</h4>
              <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                <span aria-hidden="true">&times;</span>
              </button>
            </div>
            <form class="form-horizontal" action="{{ url('/update') }}" method="post"  role="form">
                    {{ csrf_field() }}
            <div class="modal-body">  
                <div class="form-group" style="margin: 0 10px;">

              <input type="hidden" class="form-control" id="recordid" name="recordid" value="">   

              <label> Name </label><input type="text" class="form-control" id="name" name="fname" value="">
              <label>UserName: </label><select required class="form-control" id="username"  name="userName">    

               <label>Email: </label><select required class="form-control" id="email"  name="email">  


              <label>Address: </label><input type="text" class="form-control" id="address" name="address" value="">

 <label>Phone: </label><input type="text" class="form-control" id="phone" name="phone" value="">



                </div>
            </div>
                <div class="modal-footer">
                    <button type="submit" class="btn btn-success btn-xs">Update</button>
                    <button type="button" class="btn btn-secondary btn-xs" data-dismiss="modal">Cancel</button>
                </div>

                </form>
            </div>

          </div>
        </div>


//link on your view to trigger popup the modal. this link displays record gotten from db
<a onclick="updateForm('{{$record->userid}}','{{$record->name}}','{{$record->username}}','{{$record->email}}','{{$record->address}}','{{$record->phone}}')" style="cursor: pointer;"><i class="fa fa-edit"></i>Edit</a>

//javascript function to load modal and assign the record to inputes
function updateForm(r,x,y,z,w,s)
    {
        document.getElementById('recordid').value = r;
        document.getElementById('name').value = x;
        document.getElementById('username').value = y;
        document.getElementById('email').value = z;
        document.getElementById('address').value = w;
        document.getElementById('phone').value = s;


        $("#updateFormID").modal('show')
    }

//您的控制器

in your controller you have get the values and save to db

public function Update(Request $request)
{

    $recordid = $request->input('recordid');
    $name = $request->input('name');
    $username = $request->input('username');
    $email = $request->input('email');
    $address = $request->input('address');
    $phone = $request->input('phone');

    $update=DB::table('tbl')->where('id',$recordid )->update(['name' =>$name,'username' =>$username,'email' =>$email,'address'=>$address,'phone'=>$phone]);

  return redirect();


}

//route
 Route::post('/update', 'GetdataController@Update');

Note: make sure to call/import the DB facade on top. this is query builder.
use DB;