resources / views / index.blade.php
<script>
$(document).ready(function(){
$("#submit").click(function(e){
e.preventDefault();
fname = $("#name").val();
email = $("#email").val();
phone = $("#phone").val();
$.ajax({
type:"POST",
data:{"fname":fname, "email":email, "phone":phone,"_token":"{{csrf_token()}}"},
url:"{{URL::to('save')}}",
success:function(data){
$("#success").html(data);
$("#myform")[0].reset();
}
});
});
});
</script>
<div id="success"></div>
<form class="form-horizontal" id="myform">
<input type="text" class="form-control" id="name" placeholder="Enter Name">
<input type="text" class="form-control" id="email" placeholder="Enter email">
<input type="text" class="form-control" id="phone" placeholder="Enter email">
<button type="submit" class="btn btn-default" id="submit">Submit</button>
</form>
routes / web.php
<?php
Route::get('/', function(){
return view('index');
});
Route::post('save','UserController@save');
api / Http / Controllers / UserController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class UserController extends Controller
{
public function save(Request $request)
{
$name = $request->input('fname');
$email = $request->input('email');
$phone = $request->input('phone');
$data=array('fname'=>$name,"email"=>$email,"phone"=>$phone);
DB::table('emp')->insert($data);
echo "<p style='color:green;padding:0px 10px;'>Record inserted successfully</p>";
}
}
我是laravel 5的新手,现在,我试图通过jQuery将简单表单值插入数据库,这是什么问题,当我单击“提交”按钮时,它什么也没显示,即no error
,但是当我在做时检查元素并检入console
,然后显示http://localhost/freelara/save
找不到我不知道为什么?请帮我解决这个问题。
谢谢