0

beginner here. I do not understand why no alert pops up when I use the header statement,but the alert pops up just fine when I remove the header location line. Really confused :(

<?php
include "connect.php";
session_start();
// if(isset($_SESSION['user_id'])
if(isset($_POST['register'])){
echo '<script language="javascript">';
echo 'alert("message successfully sent")';
echo '</script>';
$name=$_POST["name"];
$roll=$_POST["roll"];
$year=$_POST["year"];
$pass=$_POST["register_password"];
$sql="INSERT INTO students (roll_id,name,year,password) VALUES 
('$roll','$name','$year','$pass')";
$query=mysqli_query($conn,$sql);
if(!$query)
{
 echo "Not Inserted" . mysqli_error($conn);
 }
 header("location:index.php?registered=true");
}
?>
asked Sep 9, 2018 at 5:46

3 Answers 3

1

The problem is header() needs to be sent before ANY output in order to redirect the page to another URL. At that time, you're already echoing things out, so redirects via headers wont work.

In a case such as this, (Where you want to popup a message and then redirect), you should use a javascript redirect:

echo '<script language="javascript">';
echo 'window.location.replace("/index.php?registered=true");';
echo '</script>';

This will output your popup message. After the user hits OK, the javascript redirect code will run, and redirect the page to /index.php?registered=true.

Additionally, you can add the alert box to the page that you're redirecting too. See this example index.php file:

<?php
if (isset($_GET['registered'])) {
 echo '<script>alert("You have registered!");</script>';
}
//Continue with rest of page.

If you go this route, don't include ANY output (No echo) on the register page, so that header()s are your only output. This would ideally be a better user experience, as they don't have a white popup page as they're clicking a box that says alert("message successfully sent").

answered Sep 9, 2018 at 5:49
Sign up to request clarification or add additional context in comments.

1 Comment

thank you so much I have a better understanding now! this worked like a charm, thanks for helping out a newbie :P!
1

This is because the location header redirects to a different page before the JavaScript alert is shown.

Add the alert to the destination page and it will be shown there.

answered Sep 9, 2018 at 5:49

Comments

0

Echo it like this:

echo "<script>alert('message sent'); window.location.href='url';</script>";
Lux
18.3k5 gold badges48 silver badges77 bronze badges
answered Sep 9, 2018 at 5:48

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.