简体   繁体   English

AJAX-PHP responseText将不会返回任何内容

[英]AJAX - PHP responseText won't return anything

My AJAX register script isn't working because of the responseText() function I have, in my PHP part I echo 0 to 12(and 100) depending on the if condition. 由于我具有responseText()函数,我的AJAX注册脚本无法正常工作,在PHP部分中,根据if条件,我回显0到12(和100)。

And if eg: the users username isn't valid it needs to give an error, it needs to give echo 2 and responseText then needs to give the error. 并且例如,如果:用户用户名无效,则需要给出错误,它需要给出echo 2,然后responseText然后需要给出错误。

But it won't, It will give me nothing(no var_dump, no console bug, no php_error_log, and no Apache error log). 但是不会,它什么也不会给我(没有var_dump,没有控制台错误,没有php_error_log和没有Apache错误日志)。

My Jquery/AJAX part: 我的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;
    })
});

(Sorry the code is messy) (对不起,代码很乱)

My PHP part: 我的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();
} 

(The queries work, I've put them in PHPmyAdmin and they work.) (查询有效,我将它们放在PHPmyAdmin中,它们可以工作。)

Thank you in advance! 先感谢您! :-) English isn't also my mother tongue so I'm sorry for my grammar mistakes. :-)英语也不是我的母语,所以对我的语法错误感到抱歉。

There is a far simpler way to achieve what you're trying to do here. 这里有一种简单得多的方法来实现您要执行的操作。 Instead of echo 'ing out all of these values, you should look into adding them to an array. 您应该研究将它们添加到数组中,而不是echo所有这些值。 The below is a pseudo example to explain how you'd do it. 下面是一个伪示例,说明您将如何进行操作。

$errors = array();
if(CONDITION MAKES ERROR) {
    $errors[] = 1;
} elseif(OTHER CONDITION MAKES ERROR) {
    $errors[] = 2;
}....etc

Now after you've run all of those if conditions, you can simply add this to the end of that php script: 现在,在运行所有这些if条件之后,只需将其添加到该php脚本的末尾即可:

echo json_encode($errors);

Which would ( Based on your comment ) return a json array that looks like this: 哪一个( 根据您的评论 )将返回如下所示的json数组:

[1,4,8,2]

Allowing you to loop through the responseText and echo messages for each of the errors. 允许您遍历responseText并为每个错误回显消息。


Alternatively, you could just $.trim() , something like this: 另外,您也可以只使用$.trim() ,如下所示:

$.trim(responseText);

or look at this answer to use regex (via the replace() method) instead.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM