我正在尝试在laravel框架中实现ajax,我正在尝试检查用户名的可用性,我能够通过核心php实现相同但无法通过laravel 4。
答案 0 :(得分:1)
1)您必须在控制器中定义一个方法(即UserController)
public function postEmail()
{
$userCount = User::where('email', '=', Input::get('email'))->count();
if ($userCount == 0)
{
return "true";
} else {
return "false";
}
}
2)在您的注册表单中,您必须像这样进行jQuery验证,以便在Controller方法上进行远程调用
$(document).ready(function() {
$('#registration-form').validate({
rules: {
email: {
required: true,
email: true,
remote: {
type: "POST",
url: '/user/email'
}
}
},
messages: {
email: {
remote: "The email has already been taken!"
}
},
success: function(date) {
console.log(data);
}
});