Maybe try redirect using JavaScript.
if (
user_id == 0) {
$message = 'This is a message.';
echo "<SCRIPT> //not showing me this
alert('$message')
window.location.replace('url of the page');
</SCRIPT>";
mysql_close();
}
Answer from Zander Rootman on Stack OverflowMaybe try redirect using JavaScript.
if (
user_id == 0) {
$message = 'This is a message.';
echo "<SCRIPT> //not showing me this
alert('$message')
window.location.replace('url of the page');
</SCRIPT>";
mysql_close();
}
I know this is a old post, but here is how I made it in a similar example. This example checks if the administrator has logged in from a specific IP address, if it returns false it should redirect the user back to the login page and show an error message.
User has logged in page:
if ($_SERVER['REMOTE_ADDR'] == "ip address"){
$admin = True;
}else{
session_start();
$_SESSION['errorMessage'] = "WARNING: You dont have access to this area.";
header("Location: login.php");
}
login.php:
session_start();
if(isset($_SESSION['errorMessage'])){
echo "<script type='text/javascript'>
alert('" . $_SESSION['errorMessage'] . "');
</script>";
//to not make the error message appear again after refresh:
session_unset($_SESSION['errorMessage']);
}
PHP is executed at the server side. It renders HTML/JS/CSS and sends it to the web browser, the web browser then parses and executes the JavaScript (In your case, show the alert dialog.)
However, once you call
Copyheader ("location:../SearchCountry/search.php");
The browser will be informed to redirect the user to ../SearchCountry/search.php immediately, without a chance to parse and execute the JavaScript. That's why the dialog will not show up.
Solution: redirect your user to another page with JavaScript instead of PHP.
Copy <html>
<?php
include("dbconfig.php");
$tempid = mysqli_real_escape_string($dbconfig,$_POST['tempid']);
$sql_query = "DELETE FROM Visits
WHERE visitid = '$tempid'";
$result = Mysqli_query($dbconfig,$sql_query);
if($result){
echo '<script language="javascript">';
echo 'alert("visit deleted successfully");\n';
echo 'window.location.href="../SearchCountry/search.php"'; //Redirects the user with JavaScript
echo '</script>';
die(); //Stops PHP from further execution
}
?>
</body>
</html>
Copyecho "<script>
alert('visit deleted successfully');
window.location.href='SearchCountry/search.php';
</script>";
and get rid of redirect line below.
You were mixing up two different worlds.
PHP's redirection with header("Location:xxx.php"); functionality does not allow any browser output.
You can use Javascript redirection (taking in consideration of program logic).
echo '<script>
alert(' + '"Welcome ' + $uname .'");
window.location.href="welcome.php";
</script>';
echo '<script type="text/javascript">';
echo 'alert("Welcome $uname");';
echo 'window.location.href = "index.php";';
echo '</script>';
try upper instead of below code
``echo "<script> alert('Welcome $uname')</script>";
``header("location: dashboard.php");
use urlencode()
$Message = urlencode("Some error occured please try after some time ");
header("Location:index.php?Message=".$Message);
die;
and on index.php:
if(isset($_GET['Message'])){
echo $_GET['Message'];
}
I know this is already answered years ago, I had similar problem so I found this answer... wouldn˙t this be better. I used this code in my app, with different message and $_GET variable:
header("Location:index.php?msg");
an then on index.php
if(isset($_GET['msg']))
{
$message = "Some error occured please try after some time";
echo $message;
}
That way URL is much cleaner, and header code is used as trigger for if loop.
Your problem is that you are redirecting without sending the parameters in the URL. If you want to have that data available, you need to do header("Location:thank_you.php?first_name=X"), etc. in your redirect. e.g.
$first_name = /** WHAT??? You don't have this in your code so I can't verify it exists **/
$today = date("Ymd");
$rand = strtoupper(substr(uniqid(sha1(time())),0,4));
$unique = $today . $rand;
$_SESSION['unique'] = $unique;
mail($to,$subject,$message,$headers);
mail($from,$subject2,$message2,$headers2);
// sends a copy of the message to the sender
header("Location:thank_you.php?first_name={$first_name}&unique={$unique}");
exit;
To access this on the next page, you would do:
<?php
if( isset($_GET['first_name']) && isset($_GET['unique']) ) {
echo "Mail Sent. Thank you " . $_GET['first_name' . ", we will contact you shortly.". ", Your Order Number:". $_GET['unique'];
}
Otherwise, it might just be simpler to include the thank_you.php page which will then have access to all variables within scope:
$first_name = /** WHAT??? You don't have this in your code so I can't verify it exists **/
$today = date("Ymd");
$rand = strtoupper(substr(uniqid(sha1(time())),0,4));
$unique = $today . $rand;
mail($to,$subject,$message,$headers);
mail($from,$subject2,$message2,$headers2);
// include the other file
include('thank_you.php');
exit;
In this instance, thank_you.php would be:
<?php
echo "Mail Sent. Thank you " . $first_name . ", we will contact you shortly.". ", Your Order Number:". $unique;
?>
UPDATE, per your issues...if you are having problems, try this:
php1.php
<?php
header("Location: php2.php?first_name=Joe");
exit;
php2.php
<?php
echo $_GET['first_name'];
If that doesn't work, you have issues with your PHP environment not working like mine does. If it works, the issue is with your code.
HTTP is stateless. That means that one page doesn't know anything about another page. As a result, any variables you set on the first page are long gone by the time you get redirected to the second. If you want to have the action span two different pages, then you'll need to persist the values, possibly in a database, or a session, or on the URL via GET parameters.
Do something like
header("Location: index.php?Message=" . urlencode($Message));
Then on index.php...
if (isset($_GET['Message'])) {
print $_GET['Message'];
}
In other words, index.php will always check if it's being passed a message in the url. If there is one, display it. Then, just pass the message in the redirect
if you really want to use a modal popup, generate the js...
if (isset($_GET['Message'])) {
print '<script type="text/javascript">alert("' . $_GET['Message'] . '");</script>';
}
Note that this will break if you use quotes in the message unless you escape them
<script type="text/javascript">
alert("YOUR MESSAGE HERE");
location="REDIRECTION_PAGE.php";
</script>
You can do this all on the server side by using the session:
"Show form" php script creates the form and returns it to the user.
User fills it out and submits it to another php script "receive script" which receives the form data, and notices an error, missing data, etc.
The "receive script" stores the error msg in the session (as item err) and redirects to the 'show form' script.
The "show form" script (same as in step 1) actually does more than create the form. It also:
- looks in the session to see if it has an item 'err', an error msg. In step 1 there wasn't. But now there is. So the php script creates the form, along with a div that shows the error msg to the user.
- Resets the session's 'err' item to nil.
- The php script could also include javascript in the page which would make the error msg disappear after a while or be shown as a popup, etc.
ps. The above flow is how rails handles forms and redisplay of the form.
Update: Thanks to @zod for pointing out that I wasn't clearing the err item in the session.
If an error is encountered, store the error state to a $_SESSION array and then redirect the browser to the original page. Have a script on the original page to check if an error state is set. If yes, trigger a javascript alert or whatever handling you want to have.
And at the common footer template (or at the footer of original page), check and clear the errors array, so it doesn't persist when the user moves to other pages or reloads the current page.
Example:
processor.php
<?php
if($something == $iswrong){
$_SESSION['errors']['error5301'] = 1;
session_write_close();
header("Location: http://www.example.com/originalpage.php");
exit;
} ?>
originalpage.php
<!-- Header -->
<?php
if(isset($_SESSION['errors']['error5301']) && $_SESSION['errors']['error5301'] == 1){ ?>
<script type="text/javascript">
alert('Something is not correct!');
</script>
<?php } ?>
<!-- Some page content -->
....
.....
..
......
<!-- Footer -->
<?php
if(isset($_SESSION['errors'])){
unset($_SESSION['errors']);
} ?>
Hope that helps.
I think you are trying to do this?
alert('After this goes away, I will be redirected via JavaScript');
window.location = "http://domain.com";
Ref. How do I redirect with Javascript?
To use Javascript like alert() you have to have a loaded web page. You can do the header() function then within the next request that is probably called after the alert.