Jump to content

ajax redirect doesn't work after php insert mysql query


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();
        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);

Can someone help me to fix the code? 

Link to comment
Share on other sites


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

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


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