Display message before redirect to other page
You can't do it that way. PHP header must sent out before any content, so the correct order is:
header("location: login6.php");
echo "Please Log In First";
But these codes will redirect instantly and wouldn't let you see the content.
So I would do the redirection by JavaScript:
echo "Please Log In First";
echo "<script>setTimeout(\"location.href = 'http://www.example.com';\",1500);</script>";
Show alert message before redirecting to other page ASP.NET
try this
window.location.href="Account/Login.aspx";
remove ~ operator from the path
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();
How can I show a message box on a web page before redirect to another web page in asp.net?
You'll want to do the redirect on the client side after the alert:
protected void ButtonSubmit_Click(object sender, EventArgs e)
{
ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('The data was written!'); window.location.replace('Login.aspx');", true);
}
show message before creating PHP session and redirect
What you are trying to do can be done using JavaScript. Also as noted by the commenters, you may want to either have a button or write the message on the next page. It looks like the message isn't critical, so auto-disappearing is probably not a problem:
Option 1 - JavaScript Redirect:
Use essentially the same script you have now, but use javascript to redirect.
if(mysqli_query($connect, $query)):
# Assign before message
$_SESSION['email'] = $user_email ?>
<!-- write message -->
<div class="alert alert-success" role="alert">Foi registado com sucesso!</div>
<!-- create timeout -->
<script>
setTimeout(function(){
window.location = 'areacliente.php';
}, 3000);
</script>
<?php else:
$erro .="O registo falhou!";
endif;
Option 2 - Message to Next:
Assign the session and just redirect to the next page, then show the message on that page and auto-hide it on countdown (or not).
/whatever_file_this_is.php
# Just set this as default false
$_SESSION['success'] = false;
if(mysqli_query($connect, $query)){
# Set this to true for the next page
$_SESSION['success'] = true;
# Set the email as you have it
$_SESSION['email'] = $user_email;
# Redirect
header("Location: areacliente.php");
# Stop so rest of the script doesn't run
exit;
}
else {
$erro .="O registo falhou!";
}
/areacliente.php
<?php
# Check if the session success is true
if(!empty($_SESSION['success'])):
# Remove it since it's being used now
unset($_SESSION['success']); ?>
<!-- Add an id to this div -->
<div class="alert alert-success" role="alert" id="success-msg">Foi registado com sucesso!</div>
<!-- count down and hide the message after 3 sections -->
<script>
setTimeout(function(){
document.getElementById('success-msg').style.display = 'none';
},3000);
</script>
<?php endif ?>
How do I show a message before redirect
i edited my function ShowMessage to accept 3 vars
ShowMessage(String message, WarningType type, String redirect)
and updated the method:
LiteralTypeMessage.Text = MessageType;
if (string.IsNullOrWhiteSpace(redirect) || redirect == ""){
LiteralMessage.Text = " " + message;
}
else{
LiteralMessage.Text = " " + message + ", <script>window.setTimeout(function () { $('.alert').fadeTo(1500, 0).slideUp(500, function() {$(this).remove();});window.location = '" + redirect + "'}, 3000);</script>";
}
PanelMessage.CssClass = String.Format("alert alert-{0} alert-dismissable", type.ToString().ToLower());
PanelMessage.Attributes.Add("role", "alert");
PanelMessage.Visible = true;
So when String redirect is empty it just shows the message
But when its not it shows the message and redirects to the page
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.
Related Topics
Merge 'With' and 'Wherehas' in Laravel 5
Access Variables from Parent Scope in Anonymous PHP Function
Str_Replace() on Multibyte Strings Dangerous
Display Possible Combinations of String
Using Like in Bindparam for a MySQL Pdo Query
Why Does PHP Echo'D Text Lose Its Formatting
Allow Only [A-Z][A-Z][0-9] in String Using PHP
Why Does This PHP Code Just Echo "Array"
How to Convert Boolean to String
Create an Encrypted Zip Archive with PHP
Why Is Crontab Not Executing My PHP Script
Simplest Way to Detect Client Locale in PHP
Can PHP and ASP.NET Run Together Within the Same Web Site in Iis 7.5