Jump to content

ajax redirect doesn't work after php insert mysql query


michaelfurey

Recommended Posts

I have created a registration page to access my website. After the user registrate himself should appear an alert saying that the registration was OK and a redirect to main.php page... however for some reason if I create an insert statement the alert and the redirect don't appear... If I remove the insert the alert and the redirect works... why?

This is part of the code of my 3 files:

registration.php (ajax call)

$('#submit').click(function() {
    var username2 = $('#uname2').val();
    var password2 = $('#psw2').val();
 
    $.ajax({
        url: 'ajax/response.php',
        type: 'GET',
        data: {username2 : username2, password2: password2},
            success: function(data) {
                if(data === 'correct') {
                    alert("Username and Password have been created!"); //don' work with the insert
                    location.replace("main.php"); //don' work with the insert
                    } else {
                         alert("Username or password are not correct... please register yourself!");
                   }
              }
       });
});

response.php (answer to ajax call)

if(isset($_GET['username2']) && isset($_GET['password2'])) {
    $username2 = $_GET['username2'];
    $password2 = $_GET['password2'];
 
    if (checkUser($pdo, $username2) === true) {
        echo 'duplicate';
    } else {
        insertUserPwd($pdo, $username2, $password2); //including this line the redirect and the alert doesn't work... the insert is OK
        echo 'correct';
    }
}

data_access.php (the function works but doesn't permit alert and redirect to appear)

function insertUserPwd(PDO $pdo, $usr, $pwd)
{
    $data = [
    'id' => '',
    'user' => $usr,
    'password' => $pwd
    ];
    $sql = "INSERT INTO users (id, user, password) VALUES (:id, :user, :password)";
    $stmt= $pdo->prepare($sql);
    $stmt->execute($data);
}

Can someone help me to fix the code? 

 
Link to comment
Share on other sites

Uhm I tried changing the method (post) but nothing change. In the image what happened.

The status is canceled.

When I insert that line the status is canceled furthermore the alert and the redirect don't work, even if the insert works well with the DB MySQL.

post canceled.png

Link to comment
Share on other sites

Basics:

How does your script accept input?  In your case, the code provided shows you are looking for $_GET parameters.  If you want to change this to use POST then you would need to check for $_POST parameters.

Are you clear on what runs where?  

  • PHP code is run on the server, and a response is generated and returned to the client.
  • Javascript is run within the user's browser.

This is why you need to use the developer console to look at what is happening with your ajax code when the app is run.

 

 

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.