B
B
B1oomEe2021-11-03 03:15:23
AJAX
B1oomEe, 2021-11-03 03:15:23

Why is the condition not met in JavaScript?

I am making a registration system on the page using php and ajax, and at the moment when the code should check the form for errors, it does not do this, although it should. Here is the js code:

$(document).ready(function() {
            $('#form_reg').submit(function(e) {
                e.preventDefault();
                $.ajax({
                    type: "POST",
                    url: "handle_reg.php",
                    data: $(this).serialize(),
                    success: function (result) {
                        if (result == "username-error") { 
                            console.log('username-error'); // здесь код отказывается выполнять т.к. считает, что условие неверно
                        };
                        if (result == "email-error") {
                            console.log('email-error');
                        };
                        if (result == "passwords-error") {
                            console.log("passwords-error");
                        };
                        if (result == "qwerty") {
                            console.log(result);
                            document.getElementById('sign-up-over-modal').classList.remove('visible4');
                            document.getElementById('sign-up-over-modal').classList.add('unvisible');
                            document.getElementById('email-overlay-2').classList.add('gathered');
                            document.getElementById('email-overlay-2').getAttribute(required);
                        };
                        console.log(result);
                    },
                });
            });
        });

Here is the php code:
if (mysqli_num_rows($count_username) == 1){
    echo 'username-error';
} elseif (mysqli_num_rows($count_email) == 1){
    echo 'email-error';
} elseif ($pass != $c_pass){
    echo 'passwords-error';
} elseif (mysqli_num_rows($count_username) == 0 and mysqli_num_rows($count_email) == 0 and $pass == $c_pass) {
    echo 'qwerty';
};

And the bottom line is that it is the conditions that do not work, i.e. if you put console.log(result) after them, then the console will display the value (username-error / email-error / passwords-error / qwerty), under which at least one condition must work.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
AUser0, 2021-11-03
@AUser0

So write console.log(result == "username-error"); and you will see the result of this check. And it's better to do console.dir(result);, then the real result structure will be visible, maybe the array is sitting there...

I
Ipatiev, 2021-11-03
@Fockker

I don't understand what's so hard about going into the documentation. And see there
dataType
Type: String
The type of data expected from the server. Default: Intelligent Guess (xml, json, script, text, html).
That is, not only do we have an ancient and useless jkveri, it also plays a guessing game.
Instead of which it is necessary to explicitly write in what format we expect the data.
Plus, from PHP it is better not to spit incomprehensible words, but to send in a normal format, for example json.
Well, PHP code is, of course, horror.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question