duanbo1659 2015-11-09 20:24
浏览 80
已采纳

在Laravel控制器中从AJAX post请求向MySQL添加数据

I know a lot of people are resolving this issue through jQuery, but I'm not familiar with that.

The problem is that I try to send data to MySQL using a form in html, retrieving this data with javascript, sending them to php with ajax then I try to "save" them with my controller. I got this error:

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'first_name' cannot be null

There is the code of my ajax.js:

function ajaxSaveAddClient() {
var first_name = document.getElementById("first_name").value;
var last_name = document.getElementById("last_name").value;
var phone = document.getElementById("phone").value;
var cellphone = document.getElementById("cellphone").value;
var email = document.getElementById("email").value;
var city = document.getElementById("city").value;
var stock = document.getElementById("stock").value;
var source = document.getElementById("source").value;

var newClient = new Array();
newClient["first_name"] = first_name;
newClient["last_name"] = last_name;
newClient["phone"] = phone;
newClient["cellphone"] = cellphone;
newClient["email"] = email;
newClient["city"] = city;
newClient["stock"] = stock;
newClient["source"] = source;

var token = document.getElementById("csrf_token").content;
var requestSaveAddClient = new XMLHttpRequest();
requestSaveAddClient.onreadystatechange = function() {
    console.log("orsc: ", requestSaveAddClient.readyState);
};
console.log("Before open: ", requestSaveAddClient.readyState);
requestSaveAddClient.open( "POST", "newclient", true);
requestSaveAddClient.setRequestHeader("X-CSRF-TOKEN", token);
requestSaveAddClient.send(newClient);

There is my route.php:

    Route::post('newclient', 'DashController@store');

There is my DashController.php:

public function store(Request $request)
{
    $client = new Client;
    $client -> user_id      = Auth::user()->id;
    $client -> first_name   = Request::get('first_name');
    $client -> last_name    = Request::get('last_name');
    $client -> phone        = Request::get('phone');
    $client -> cellphone    = Request::get('cellphone');
    $client -> email        = Request::get('email');
    $client -> city         = Request::get('city');
    $client -> stock        = Request::get('stock');
    $client -> source       = Request::get('source');
    $client -> save();
    die();
}

dahsboard.blade.php :

                <div>
                <table id="addClientTable" name="addClientTable" class="addClientTable">
                    <meta name="csrf-token" id="csrf_token" content="{{ csrf_token() }}">
                    <tr>
                        <th class="rightCellCT">
                            NOM :
                        </th>
                        <th class="centerLargeInputCT">
                            <input id="last_name" type="text"/>
                        </th>
                        <th class="rightCellCT">
                            TÉLÉPHONE :
                        </th>
                        <th class="centerSmallInputCT">
                            <input id="phone" type="text"/>
                        </th>
                        <th class="rightCellCT">
                            VILLE :
                        </th>
                        <th class="centerLargeInputCT">
                            <input id="city" type="text"/>
                        </th>
                        <th class="rightCellCT">
                            STOCK :
                        </th>
                        <th class="centerSmallInputCT">
                            <input id="stock" type="text"/>
                        </th>
                    </tr>
                    <tr>
                        <td class="rightCellCT">
                            PRÉNOM :
                        </td>
                        <td class="centerLargeInputCT">
                            <input id="first_name" type="text"/>
                        </td>
                        <td class="rightCellCT">
                            CELLULAIRE :
                        </td>
                        <td class="centerSmallInputCT">
                            <input id="cellphone" type="text"/>
                        </td>
                        <td class="rightCellCT">
                            COURRIEL :
                        </td>
                        <td class="centerLargeInputCT">
                            <input id="email" type="text"/>
                        </td>
                        <td class="rightCellCT">
                            SOURCE :
                        </td>
                        <td class="centerSmallInputCT">
                            <input id="source" type="text"/>
                        </td>
                    </tr>
                </table>
            </div>
  • 写回答

2条回答 默认 最新

  • dongsuichi6529 2015-11-09 20:52
    关注

    The problem is that the XmlHttpRequest send method cannot use an array like you are passing it, so the params you try to send to do not make it to the controller.

    A simple alternative might be using the FormData object. A quick example might look like this:

    var token = document.getElementById("csrf_token").content,
        requestSaveAddClient = new XMLHttpRequest(),
        newClient = new FormData();
    
    requestSaveAddClient.open( "POST", "newclient", true);
    requestSaveAddClient.setRequestHeader("X-CSRF-TOKEN", token);
    newClient.append("first_name", first_name);
    //etc...
    requestSaveAddClient.send(newClient);
    

    I would also strong suggest you do a bit more research into using jQuery or something like it to help you write the scripts for your site, there's a bit more of a learning curve but it will save you a lot of time and heart ache in the future and will make you a better developer overall.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Bibtex4Word 引用中文文献
  • ¥20 用opencv c/c++ 转换成灰度图,然后做一下直方图均衡,输出mp4文件
  • ¥20 matlab中的双层数值积分
  • ¥50 服务器打印水晶报表问题
  • ¥30 gradle环境下javafx项目如何使用druid连接池
  • ¥15 服务器打印水晶报表问题
  • ¥18 深度学习tensorflow1,ssdv1,coco数据集训练一个模型
  • ¥100 关于注册表摄像头和麦克风的问题
  • ¥30 代码本地运行正常,但是TOMCAT部署时闪退
  • ¥15 关于#python#的问题