我正在尝试验证 Silverstripe 3 中的用户名字段。Ajax 检查用户名是否存在,如果存在,则通过 ajax 显示“此名称已被采用”。
但问题是,没有值通过 Ajax 传递给变量。$_POST['username'] 为空。
所以我在控制器中的代码:
public function checkuser1(){
$Params = $this->getURLParams();
if($Params['ID'] == '1'){
if (Director::is_ajax()) {
$username = $_POST['username']; // Seems that I dont receive any value from input vie ajax
$username = mysql_real_escape_string($username);
$check_for_username = mysql_query("SELECT ID FROM Member WHERE UserName='$username'");
if(mysql_num_rows($check_for_username))
{
return '1';
}
else
{
return '0';
}
}
else {
return Director::redirect(Director::baseURL());
}
}
}
public function checkuser(){
return $this->checkuser1();
}
和JS代码:
<script type="text/javascript">
$(document).ready(function(){
$("#Form_RegistrationForm_UserName").change(function(){
$(".container .inner .top .right").html("<img src='ajax-loader.gif' /> checking...");
var username = $("#Form_RegistrationForm_UserName").val();
var dataString = 'username='+ username;
$.ajax({
type:"post",
url:"http://localhost/mysite/member/checkuser/1/",
data:dataString,
success:function(server_response){
if(server_response == '0'){
$(".container .inner .top .right").html("Username available");
}
else{
$(".container .inner .top .right").html("Username already taken");
}
}
});
});
});
</script>