由于我的responseText()
函数,我的AJAX寄存器脚本不起作用,在我的PHP部分中,我根据if条件回显0到12(和100)。
如果例如:用户的用户名不是有效的,它需要给出错误,它需要给出echo 2,然后responseText需要给出错误。
但它不会,它不会给我任何东西(没有var_dump,没有控制台错误,没有php_error_log,也没有Apache错误日志)。
我的Jquery / AJAX部分:
if(username.val() != '' && password.val() != '' && password2.val() != '' && email.val() != '' && terms.val != '')
{
var UrlToPass = 'action=aanmelden&username='+username.val()+'&password='+password.val()+'&password2='+password2.val()+'&email='+email.val()+'&av='+terms.val();
$.ajax({
type : 'POST',
data : UrlToPass,
url : '/outgame/register.php',
success : function(responseText)
{
if(responseText == 0)
{
$.notify('U hebt geen gebruikersnaam opgegeven!', 'error');
}
else if(responseText == 1)
{
$.notify('Uw gebruikersnaam moet meer dan 3 en minder dan 16 karakters bevatten!', 'error');
}
else if(responseText == 2)
{
$.notify('Uw gebruikersnaam bevat ongeldige tekens!', 'error');
}
else if(responseText == 3)
{
$.notify('U hebt geen wachtwoord opgegeven!', 'error');
}
else if(responseText == 4)
{
$.notify('Uw wachtwoord moet meer dan 6 en minder dan 16 karakters bevatten!', 'error');
}
else if(responseText == 5)
{
$.notify('U hebt geen tweede wachtwoord opgegeven!', 'error');
}
else if(responseText == 6)
{
$.notify('De wachtwoorden komen niet met elkaar overeen!', 'error');
}
else if(responseText == 7)
{
$.notify('U hebt geen mail adres opgegeven!', 'error');
}
else if(responseText == 8)
{
$.notify('Uw mail adres is niet geldig!', 'error');
}
else if(responseText == 9)
{
$.notify('Uw gebruikersnaam is al ingebruik!', 'error');
}
else if(responseText == 10)
{
$.notify('Er is al een account aagemaakt met dit email adres!', 'error');
}
else if(responseText == 11)
{
$.notify('U hebt onze AV niet geaccepteerd!', 'error');
}
else if(responseText == 12)
{
$.notify('Uw invoer voor onze AV is ongeldig!', 'error');
}
else if(responseText == 100)
{
$.notify('U bent succesvol geregistreerd', 'success');
}
else
{
alert('error!');
}
}
});
}
return false;
})
});
(抱歉,代码很乱)
我的PHP部分:
if(isset($_POST['action']) && $_POST['action'] == 'aanmelden')
{
$stringUsername = trim($_POST['username']);
$stringPassword = trim($_POST['password']);
$stringPassword2 = trim($_POST['password2']);
$stringEmail = trim($_POST['email']);
$stringTerms = trim($_POST['av']);
$bolean = false;
$stringsmallUser = strtolower($stringUsername);
$stmt = $mysqli->prepare("SELECT username FROM users WHERE username = ?");
$stmt->bind_param('s', $stringsmallUser);
$stmt->execute();
$intMatchu = $stmt->num_rows();
$stmt->close();
$stmt = $mysqli->prepare("SELECT email FROM users WHERE email = ?");
$stmt->bind_param('s', $stringEmail);
$stmt->execute();
$intMatche = $stmt->num_rows();
$stmt->close();
if(empty($stringUsername))
{
echo 0;
$bolean = true;
}
elseif(strlen($stringUsername) < 3 || strlen($stringUsername) > 16)
{
echo 1;
$bolean = true;
}
elseif(!ctype_alnum($stringUsername))
{
echo 2;
$bolean = true;
}
if(empty($stringPassword))
{
echo 3;
$bolean = true;
}
elseif(strlen($stringPassword) < 6 || strlen($stringPassword) > 16)
{
echo 4;
$bolean = true;
}
if(empty($stringPassword2))
{
echo 5;
$bolean = true;
}
elseif($stringPassword != $stringPassword2)
{
echo 6;
$bolean = true;
}
if(empty($stringEmail))
{
echo 7;
$bolean = true;
}
elseif(!filter_var($stringEmail, FILTER_VALIDATE_EMAIL))
{
echo 8;
$bolean = true;
}
if($intMatchu != 0)
{
echo 9;
$bolean = true;
}
elseif($intMatchu != 0)
{
echo 10;
$bolean = true;
}
if(empty($stringTerms))
{
echo 11;
$bolean = true;
}
elseif($stringTerms == 'avok')
{
echo 12;
$bolean = true;
}
if($bolean == false)
{
echo 100;
}
}
else
{
exit();
}
(查询有效,我已将它们放入PHPmyAdmin并且它们可以工作。)
提前谢谢! :-)英语不是我的母语,所以我很抱歉我的语法错误。
答案 0 :(得分:2)
有一种更简单的方法可以实现您在此尝试做的事情。
您应该考虑将它们添加到数组中,而不是echo
列出所有这些值。以下是一个虚拟的例子来解释你是如何做到的。
$errors = array();
if(CONDITION MAKES ERROR) {
$errors[] = 1;
} elseif(OTHER CONDITION MAKES ERROR) {
$errors[] = 2;
}....etc
现在,在您运行了所有这些if
条件后,您可以将其添加到该php脚本的末尾:
echo json_encode($errors);
哪个(Based on your comment)会返回一个json
数组,如下所示:
[1,4,8,2]
允许您遍历每个错误的responseText
和echo消息。
或者,您可以$.trim()
,就像这样:
$.trim(responseText);
or look at this answer to use regex (via the replace() method) instead.