简体   繁体   中英

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.

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.

But it won't, It will give me nothing(no var_dump, no console bug, no php_error_log, and no Apache error log).

My Jquery/AJAX part:

    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:

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.)

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. 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:

echo json_encode($errors);

Which would ( Based on your comment ) return a json array that looks like this:

[1,4,8,2]

Allowing you to loop through the responseText and echo messages for each of the errors.


Alternatively, you could just $.trim() , something like this:

$.trim(responseText);

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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