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']);
}
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.
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");
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.
If you redirect with the header() function, the browser will immediately redirect and no other data will be outputted for the browser.
You could alert() first and perform a redirect with javascript, e.g. with
window.location = "http://www.google.com/"
Read more about the header() function: PHP.net header()
You can't both print to the screen and redirect at the same time. header() requires that no output has been sent, and since you're echoh:ing your JavaScript alert, the redirection & JavaScript execution won't be run as you're expecting it to.
Solution? Maybe pass some sort of message as a GET parameter, and do the alert on student_detail_entry.php instead (on page load).
If you redirect with the header() function, the browser will immediately redirect and no other data will be outputted for the browser.
You could alert() first and perform a redirect with javascript, e.g. with
window.location = "http://www.google.com/"
Read more about the header() function: PHP.net header()
You can't both print to the screen and redirect at the same time. header() requires that no output has been sent, and since you're echoh:ing your JavaScript alert, the redirection & JavaScript execution won't be run as you're expecting it to.
Solution? Maybe pass some sort of message as a GET parameter, and do the alert on student_detail_entry.php instead (on page load).
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>