简体   繁体   English

Ajax表单数据未保存在数据库中

[英]Ajax form data not saved in database

I am trying to save user input into database using Ajax in Laravel-5.2. 我试图在Laravel-5.2中使用Ajax将用户输入保存到数据库中。

This is my route.php 这是我的route.php

Route::get('xxxxx/{task_id?}',function($task_id){
$task = App\XXXXX::find($task_id);

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

    Route::put('xxxxx/{task_id?}',function(Request $request,$task_id){
        $task = App\XXXXX::find($task_id);

        $task->Name = $request->Name;//
        $task->Email = $request->Email;
        $task->Telephone = $request->Telephone;

        $task->save();

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

In my view, the save button is used as. 在我看来,保存按钮用作。

<div class="modal-footer">
   <button type="button" class="btn btn-primary" id="btn-save" value="update">Save changes</button>
       <input type="hidden" id="task_id" name="task_id" value="0">
</div>

my js file created using this tutorial. 使用本教程创建的我的js文件 .
I am getting the popup, Save button is not working. 我收到弹出窗口,“ Save按钮不起作用。
What is the wrong here? 这是怎么了 I m new for Ajax. 我是Ajax的新手。
Thanks in advance. 提前致谢。

This is route.php 这是route.php

Route::match(['get','post'], 'my/save-data','MyController@SaveData'); 路线:: match(['get','post'],'my / save-data','MyController @ SaveData');

This is your html: 这是您的html:

Save changes 保存更改

This is your Controller file: MyController.php 这是您的Controller文件:MyController.php

public function SaveData( Request $request ) { $input = $request->all(); 公共功能SaveData(Request $ request){$ input = $ request-> all();

  try{ // You can now use the Subscribe model without its namespace // as you referenced it by its namespace in a use statement. $subscribe = new Subscribe(); // If you want to use a class that is not referenced in a use // statement then you must reference it by its full namespace. $otherModel = new \\App\\Models\\Other\\Namespace\\OtherModel(); $otherModel = $input['Name']; $otherModel = $input['Email']; $otherModel = $input['Telephone']; // save $otherModel->save(); } catch (\\Illuminate\\Database\\Eloquent\\ModelNotFoundException $e) { \\Log::error( $e->getMessage(), $context ); } catch (Exception $e){ \\Log::error( $e->getMessage(), $context); } return response()->json( ['status'=>'success', 'message'=>'Completed successfully'] ); } 

This is your Js file:save.js 这是您的Js文件:save.js

function save() { getData = { name: "value", // from get eliment 函数save(){getData = {name:“ value”,//来自get element
email: "value", // from get eliment telephone: "value" // from get eliment }; 电子邮件:“ value”,//来自eliment电话:“ value” //来自eliment};

 $.ajax({ type: 'post', // POST Request url: 'localhost/my/save-data', // localhost/my/save-data // Url of the Route (in this case user/save not only save) data: getData, // Serialized Data beforeSend: function (xhr) { // Function needed from Laravel because of the CSRF Middleware var token = $('meta[name="csrf_token"]').attr('content'); if (token) { return xhr.setRequestHeader('X-CSRF-TOKEN', token); } }, success: function (data) { // Successfuly called the Controler // Check if the logic was successful or not if (data.status == 'success') { console.log('alles ok'); } else { console.log(data.msg); } }, error: function (data) { // Error while calling the controller (HTTP Response Code different as 200 OK console.log('Error:', data); } }); 

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM