2

I've been wanting to post a javascript alert (a pop up window w/ a message) to simply say "Thanks! You've been added!". Once a user has entered in the details in the form and clicks the submit button.

I have googled how to do this, however when adding it, it never worked and have tried different ways but am getting stumped on how to do it now. If anyone knows what I am doing wrong then any help would be much appreciated.

I would also like to note this in a separate php file.

Below you will find my code;

insert.php

<?php
$con=mysqli_connect("localhost","cl51-main-3i6","password","cl51-main-3i6");
// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$firstname = mysqli_real_escape_string($con, $_POST['firstname']);
$lastname = mysqli_real_escape_string($con, $_POST['lastname']);
$email = mysqli_real_escape_string($con, $_POST['email']);

$sql="INSERT INTO emails ( Firstname, Lastname, Email) VALUES ( '$firstname', '$lastname', '$email')";

if (!mysqli_query($con,$sql)) {
    die('Error : ' . mysql_error($con));
}

echo '<script language="javascript">';
echo 'alert("Thank you! You've now joined the e-mail club!")';
echo '</script>';

header("Location: http://isometricstudios.co.uk/news.html");
exit;

mysqli_close($con);
?>
2
  • First off, you cannot alter the header() after sending html content out to the page. if you are doing this what I would recommend is forwarding them to the success page with a $_GET value stating success header("Location: http://isometricstudios.co.uk?success=true"), then on the landing page, if($_GET['success'] == 'true') put your alert code Commented Oct 7, 2014 at 21:20
  • 1
    given that your echo line with the alert has php syntax errors, you'll never even get to the header() line to trigger the redirect... Commented Oct 7, 2014 at 21:23

5 Answers 5

1

The problem is because the header() will execute the redirection immediately. use window.location in <script> instead of header(location)

<?php
$con=mysqli_connect("localhost","cl51-main-3i6","password","cl51-main-3i6");
// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$firstname = mysqli_real_escape_string($con, $_POST['firstname']);
$lastname = mysqli_real_escape_string($con, $_POST['lastname']);
$email = mysqli_real_escape_string($con, $_POST['email']);

$sql="INSERT INTO emails ( Firstname, Lastname, Email) VALUES ( '$firstname', '$lastname', '$email')";

if (!mysqli_query($con,$sql)) {
    die('Error : ' . mysql_error($con));
}

echo '<script language="javascript">';
echo 'alert("Thank you! You've now joined the e-mail club!")';
echo 'window.location.href="http://isometricstudios.co.uk/news.html";';
echo '</script>';

exit;

mysqli_close($con);
?>

Now the alert will be displayed and redirected only when ok button is pressed

Sign up to request clarification or add additional context in comments.

Comments

0

Did you scaped the ' in your string?

echo 'alert("Thank you! You\'ve now joined the e-mail club!")';

Comments

0

You have to escape single quotes in your string which you echo:

echo 'alert("Thank you! You\'ve now joined the e-mail club!")';

Comments

0

Your code is fine apart from one line

 echo 'alert("Thank you! You've now joined the e-mail club!")';

You are using 's to quote the line but you have a ' in you've. Write "You have" instead. :)

echo 'alert("Thank you! You have now joined the e-mail club!")';

You could also escape it as the other answerers said but it makes the code slightly messier in my opinnion

Comments

0

If you redirect the browser with a Location Header, any javascript code on the page itself is not executed, because the page you're redirecting to is loaded instead.

Also, I don't think you can echo or otherwise output any content before calling the header function.

If you want to show an alert, do it on the page you're redirecting to, or, if that's not under your control, redirect using javascript instead of with the Location header:

document.location = 'http://isometricstudios.co.uk/news.html';

Also, it's probably easier to not use echo for writing javascript, because then you'd have to escape quotes, which you failed to do in

echo 'alert("Thank you! You've now joined the e-mail club!")';

It's easier to just keep javascript outside your php tags, so something like

<?php
(do server-side php stuff)
?>

<script>
(do client-side javascript stuff)
</script>

<?php
(do more server-side php stuff)
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.