我知道很多人都是通过jQuery解决这个问题,但我对此并不熟悉。
问题是我尝试使用html中的表单将数据发送到MySQL,使用javascript检索此数据,使用ajax将它们发送到php然后我尝试使用我的控制器“保存”它们。我收到了这个错误:
SQLSTATE [23000]:完整性约束违规:1048列'first_name'不能为空
我的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);
有我的route.php:
Route::post('newclient', 'DashController@store');
有我的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>
答案 0 :(得分:1)
问题是XmlHttpRequest发送方法不能像你传递的那样使用数组,所以你尝试发送的参数不会进入控制器。
一个简单的替代方案可能是使用FormData对象。一个简单的例子可能如下所示:
var data = [
{"year":2015, values:[1,2,3]},
{"year":2016, values:[10,20,30]},
{"year":2017, values:[100,200,300]}
];
我还强烈建议你做一些关于使用jQuery或类似东西的研究,以帮助你为你的网站编写脚本,这里有更多的学习曲线,但它会为你节省很多未来的时间和心痛,将使你成为一个更好的开发者。
答案 1 :(得分:0)
将Request::get('some_name')
更改为Request::input('some_name');
。对store
方法中的所有输入执行此操作。
事实上,我会把这个功能清理干净......就像下面这样。我不会触及实际的javascript。它也不好。
$request->merge([
'user_id' => Auth::user()->id
]);
$task = Client::create($request->all());