Jump to content
michaelfurey

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();
 
    $.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? 

 

Share this post


Link to post
Share on other sites

Use your browser to watch the AJAX request happen, and see what the response to it was.

Share this post


Link to post
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

Share this post


Link to post
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.

 

 

Share this post


Link to post
Share on other sites

And is your #submit a regular submit button inside a form? A button that will try to submit that form when clicked?

Share this post


Link to post
Share on other sites

Along with everything else. You need to echo a properly formatted json string. 

 

echo "{'message':'correct'}";

then 

if(data.message === 'correct')

 

Share this post


Link to post
Share on other sites

Join the conversation

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

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