使用Laravel 5.7和Ajax插入数据并显示在以下刀片文件中,
<table border="0">
<th colspan="6">Members</th>
<tr>
<td>Id</td>
<td>Name</td>
<td>Email</td>
<td>Address</td>
</tr>
@foreach($member as $value)
<tr>
<td>{{$value->id}}</td>
<td>{{$value->name}}</td>
<td>{{$value->age}}</td>
<td>{{$value->email}}</td>
<td>{{$value->address}}</td>
</tr>
@endforeach
</table>
<!--insert data-->
<table border="0">
<th colspan="2">Insert</th>
<tr>
<td>Name:</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>Age:</td>
<td><input type="text" name="age"></td>
</tr>
<tr>
<td>Email:</td>
<td><input type="text" name="email"></td>
</tr>
<tr>
<td>Address:</td>
<td><input type="text" name="address"></td>
</tr>
<tr>
<td colspan="2"><button type="submit" id="insert">Insert</button></td>
</tr>
</table>
{{ csrf_field() }}
$('#insert').click(function(){
$.ajax({
type:'post',
url:'insertdata',
data:{
'_token':$('input[name:_token').val(),
'name':$('input[name:name').val(),
'age':$('input[name:age').val(),
'email':$('input[name:email').val(),
'address':$('input[name:address').val(),
},
success:function(data){
window.location.reload();
},
});
});
我的HomeController就是这样,
public function index()
{
$member = membersmodel::all();
return view('home.index')->with('member',$member);
}
public function insertdata(Request $request)
{
$member = New membersmodel();
$member->name = $request->name;
$member->age = $request->age;
$member->email = $request->email;
$member->address = $request->address;
$member->save();
return response()->json($member);
}
路线是
Route::get('/','HomeController@index');
Route::post('insertdata','HomeController@insertdata');
但是当我在插入表中插入数据并单击插入按钮时,什么都不会发生。不保存数据或不显示。不,发生任何错误。如何解决此问题?
答案 0 :(得分:0)
您必须忘记在模型membersmodel
中提及可填充字段