PHP - AJAX - mysql - 无法将数据插入数据库(不使用AJAX时工作正常)

I have this "register users" file in which I have a form, I'll simplify in here what I have:

 <form action="" method="POST">
    <label for="user" class="control-label">User </label>
    <input type="text" name="user" class="form-control" id="user"  value="" required=""/>
    <label for="user" class="control-label">Password1 </label>
    <input type="text" name="password1" class="form-control" id="password1"  value="" required=""/>
    <label for="user" class="control-label">Password2 </label>
    <input type="text" name="password2" class="form-control" id="password2"  value="" required=""/>
    <button type="button" value="signUp" name="submit" class="btn btn-lg btn-primary btn-block" onClick="register()">Sign up!</button>

As you can see, there is an event in there, in a JS file. This file has all the vaidations of the inputs and it works just fine (I don't think it's relevant, so I won't post it). It also has the AJAX call to the PHP file that will insert the data into the database.

 function register(){

 if(validationRegister()){
        $.ajax({

                url: "http://localhost/myProject/extras/processSignUp.php", 
                type: "POST",
                data: {"user": user,
                       "password": password,
                       "password2": password2,
                       },
                dataType: "html",
                cache: false,
                beforeSend: function() {    
                    console.log("Processing...");
                },
                success: 
                      function(data){
                        if(data == "Registered"){
                           window.location.href = "http://localhost/myProject/index.php";
                        }else{
                           window.location.href = "http://localhost/myProject/signUp.php";
                        }
                    }

    });

}else{
    alert("Incorrect data");
}

}

And this is the PHP file:

 <?php

    include_once "connection.php"; --> this has all the data for the connection to the database 

    if($_POST['user'] == '' || $_POST['password'] == '' || $_POST['password2'] == ''){ 
        echo 'Fill all the information'; 
    }else{ 

        $sql = 'SELECT * FROM `user`'; 
        $rec = mysqli_query($con, $sql); 
        $verify_user = 0; 

        while($result = mysqli_fetch_object($rec)){ 
            if($result->user == $_POST['user']){ 
                $verify_user = 1; 
            } 
        } 

        if($verify_user == 0){ 
            if($_POST['password'] == $_POST['password2']){ 
                $user = $_POST['user']; 
                $password = $_POST['password'];  
                $sql = "INSERT INTO user (user,password) VALUES ('$user','$password')";
                mysqli_query($con, $sql); 

                echo "Registered"; 
            }else{ 
                echo "Passwords do not match"; 
            } 
        }else{ 
            echo "This user has already been registered"; 
        }
    } 

?>

The PHP code, works great when used on its own (it used to be at the beginning of the form file, surrounded by if($_POST['submit']){}) But now I want to use it in a separate file, and use AJAX, and I'm unable to register a user :/ the value of data is never "Registered"... Any ideas?

Thanks in advance! :)

Please never run this code in a live environment, your code is open to SQL injection and you NEED to hash passwords.

This line:

if($_POST['user'] == '' or $_POST['password']){ 

Looks to be your issue. You want to be testing $_POST['password'] somehow, like $_POST['password'] == '' or !isset($_POST['password']).

Your logic is also horribly constructed, you may want to go look at a few tutorials. e.g. why are you fetching ALL your users just to test if one exists, do that logic in the SQL code itself to avoid fetching an entire table for no reason.