michaelfurey Posted April 21, 2019 Share Posted April 21, 2019 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? Quote Link to comment Share on other sites More sharing options...
requinix Posted April 22, 2019 Share Posted April 22, 2019 Use your browser to watch the AJAX request happen, and see what the response to it was. Quote Link to comment Share on other sites More sharing options...
michaelfurey Posted April 22, 2019 Author Share Posted April 22, 2019 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. Quote Link to comment Share on other sites More sharing options...
gizmola Posted April 22, 2019 Share Posted April 22, 2019 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. Quote Link to comment Share on other sites More sharing options...
requinix Posted April 22, 2019 Share Posted April 22, 2019 And is your #submit a regular submit button inside a form? A button that will try to submit that form when clicked? Quote Link to comment Share on other sites More sharing options...
taquitosensei Posted April 23, 2019 Share Posted April 23, 2019 Along with everything else. You need to echo a properly formatted json string. echo "{'message':'correct'}"; then if(data.message === 'correct') Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.