Reputation: 279
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);
?>
Upvotes: 2
Views: 533
Reputation: 1191
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
Upvotes: 1
Reputation: 1054
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)
?>
Upvotes: 0
Reputation: 6081
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
Upvotes: 0
Reputation: 3327
You have to escape single quotes in your string which you echo:
echo 'alert("Thank you! You\'ve now joined the e-mail club!")';
Upvotes: 0
Reputation: 150
Did you scaped the ' in your string?
echo 'alert("Thank you! You\'ve now joined the e-mail club!")';
Upvotes: 0