Reputation: 13
I need to show a alert dialog from php if registration successfull. As well as I want to redirect to another page. my php code is here:
if($conn->query($sql)===TRUE){
echo "<script>alert('Registration Successfull');</script>";
header('Location: index.php');
exit();
}
but I get an warning message which says:
Warning: Cannot modify header information - headers already sent by (output started at /home/bdacademichelp/public_html/medcino.com/signup_back.php:19) in /home/bdacademichelp/public_html/medcino.com/signup_back.php on line 20
line 19 is the echo line which shows the alert message
Upvotes: 0
Views: 1119
Reputation: 3440
Using a javascript alert is going to delay the redirect until the user clicks OK. What you probably want to do is display the alert in your index.php when there has been a successful subscription. There are a few different ways of passing this information but the most common way is to pass the success message in the session.
// page1.php
<?php
session_start();
if($conn->query($sql)===TRUE){
$_SESSION['message'] = "Registration successful";
header('Location: index.php');
exit();
}
session_write_close ();
?>
// index.php
<?php
session_start();
if (isset($_SESSION['message'])) {
$show_message = $_SESSION['message'];
$_SESSION['message'] = null;
}
session_write_close ();
// ...
if (isset($show_message)) {
echo "<script>alert('{$show_message}');</script>";
}
Other alternatives are to pass the data in the URL such as index.php?message=Registration%20Successful or passing the message in a cookie.
Upvotes: 2
Reputation: 5322
you can print message in alert and redirect as below:
echo "<script>alert('Success');document.location='index.php'</script>";
In your code you already send something as echo so you cannot use header after that.
Upvotes: 1