How to Display a Success Message After Redirecting the User Back to My Home Page

How do I display a success message after redirecting the user back to my home page?

Use sessions so it doesn't show again when you refresh the page.

Serverside:

$_SESSION['success'] = 'Registered!';
echo
"<script type=\"text/javascript\">
window.location.href='../index.php';
</script>";
exit();

Homepage:

if (isset($_SESSION['success']) && ! empty($_SESSION['success'])) {
echo htmlentities($_SESSION['success']);
unset($_SESSION['success']);
}

Make sure you initialise sessions with session_start(); before trying to manipulate them

displaying a message after redirecting the user to another web page

Header( 'Location: Database.php?success=1' );

And in the Database.php page :

if ( isset($_GET['success']) && $_GET['success'] == 1 )
{
// treat the succes case ex:
echo "Success";
}

How can i display a success pop up message before redirecting a user to the last php page

You cannot output anything before a header() call as it will cause header already sent error.

Made the redirection using a js code.

Try this:

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$sql = "INSERT INTO comment (Comment, firstname, lastname, Image_path, Approved)
VALUES ('$comment', '$firstname','$lastname','$target_file','2')";

if ($conn->query($sql) === TRUE) {
$message = "Thankyou for your comment.";
echo "<script type='text/javascript'>alert('$message');</script>";
echo "<script type='text/javascript'>
window.location = '".$_SERVER['HTTP_REFERER']."';
</script>";

} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}

$conn->close();

Display alert message and redirect after click on accept

echo "<script>
alert('There are no fields to generate a report');
window.location.href='admin/ahm/panel';
</script>";

and get rid of redirect line below.

You were mixing up two different worlds.

Showing message after redirect

An elegant and common approach is to use a flash message. This is basically a one-off session entry which is set in one request and is delivered in the next one, then instantly deleted. Consider the following example.

On the redirecting page, you set the flash message in the session object:

$_SESSION["flash"] = ["type" => "success", "message" => "You are great!"];

On a target page, check if there's a flash message. If so, output and delete it.

if (isset($_SESSION["flash"]))
{
vprintf("<p class='flash %s'>%s</p>", $_SESSION["flash"]);
unset($_SESSION["flash"]);
}

Note: The example assumes you are using PHP sessions already. If not, you must start the session first.

Redirecting to same page along with Success Message with Php

You could skip the session, and pass a url query parameter as a code or the message.

$stmt->execute() or die(mysqli_error($db)); 

if($stmt){
// echo "Data Submitted succesfully";
header('Location: ' . $_SERVER['HTTP_REFERER'] . '?message=success1');
exit;
}
$stmt->close();
$db->close();
}

Then have code that checks for $_GET['message] ...etc

Transfering the success message to the redirected main page in ajax (native JS/not JQUERY)

you can use session to tranfer message then catch by js function call it flash

save your edit result in $_SESSION['msg'] and $_SESSION['class'] to control msg color by bootstrap

in your php code

    function flash() {
$message = $_SESSION['msg'];
$class = $_SESSION['class'];
if(!empty($message)){
echo '<div class="bg-'.$class.' msg-flash alert alert-warning alert-dismissible fade show" role="alert"
style="transition: all .9s ease; width:100%;" >'.$message.'<button type="button" class="close white "
data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button></div>';
}
// empty $message after show
$message = "";
}

in your javascript code

    function flash() {
// collect result from php session msg by http request
}

run function flash after every function in your javascript files and after dom loaded

How to display a success message upon redirection from controller in Laravel?

return redirect('/')->with('message', 'Message sent!');

Read more about redirects in Laravel.

Be sure that Session is working properly to get flash message working.

how to show alert in the redirected page?

Solution1

Before redirecting, you should create a session object and save the alert messages in sessions.

start_session()

$_SESSION["alert"] = ["type" => "success", "message" => "User deleted successfully"];

On index.php page

On a index.php page, check if there's a flash/alert message. If so, output and delete it.

if (isset($_SESSION["alert"]))
{

echo

"<div class='alert alert-success alert-dismissible'>
<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>×</button>
<h4><i class='icon fa fa-check'></i> Alert!</h4>
$_SESSION["alert"]
</div>";

unset($_SESSION["flash"]);
}

Thats it.



Related Topics



Leave a reply



Submit