首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Ajax表单数据未保存在数据库中

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

Stack Overflow用户
提问于 2016-09-19 17:50:50
回答 1查看 465关注 0票数 0

我正在尝试使用Laravel-5.2中的Ajax将用户输入保存到数据库中。

这是我的route.php

代码语言:javascript
复制
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);
    });

在我看来,保存按钮用作。

代码语言:javascript
复制
<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>

我使用this tutorial.创建的js文件。

我正在得到弹出窗口,Save按钮不工作。

这里出了什么问题?我是Ajax的新手。

提前谢谢。

EN

回答 1

Stack Overflow用户

发布于 2016-09-19 18:34:45

这是route.php

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

这是你的

代码语言:javascript
复制
  Save changes           

这是您的控制器文件: MyController.php

公共函数SaveData( Request $request ){ $input = $request->all();

代码语言:javascript
复制
    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'] );
}

这是你的Js file:save.js

函数save() { getData ={ name:"value",// from get eliment

代码语言:javascript
复制
                 email: "value", // from get eliment                     telephone: "value" // from get eliment                 };
代码语言:javascript
复制
$.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);
    }
});

}

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39570145

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档