简体   繁体   中英

php redirecting to a page with alert dialog

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM