user3565273
user3565273

Reputation: 65

Javascript alert and php header

I got a little problem. When I got my PHP script without header it's fine, I am getting javascript alert box. But when I use header before alert it's not working. It's redirecting me like it should, but it's not showing any box. Could someone help me?

if ( $pkt < 1 OR $user_id == 0) {
    header("Location: http://dunno.com/file.php");
    $message = 'This is a message.';

echo "<SCRIPT> //not showing me this
alert('$message');
</SCRIPT>";
    mysql_close();
}

And here's the one which work (but without heading)

if ( $pkt < 1 OR $user_id == 0) {
    $message = 'This is a message.';

echo "<SCRIPT> //showing me
alert('$message');
</SCRIPT>";
    mysql_close();
}

I would be thankful for help :)

@edit Maybe is there any command to make alert wait until whole browser load? (if it's the problem)

Upvotes: 5

Views: 45780

Answers (7)

Zander Rootman
Zander Rootman

Reputation: 2208

Maybe try redirect using JavaScript.

if ( $pkt < 1 OR $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();
}

Upvotes: 5

saltcracker
saltcracker

Reputation: 321

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']);
}

Upvotes: 2

llanato
llanato

Reputation: 2491

The below header function will run a 302 redirect by default and redirect the client to http://dunno.com/file.php so any code after the code begin processed after the header function will not be seen as the client is redirected from the page this code is on.

 header("Location: http://dunno.com/file.php");

Please read up further on the header function here: http://www.php.net/manual/en/function.header.php

You could make you program sleep/wait for a few seconds(lets say 10 seconds for this example) before redirecting if you need the message to display like so:

if ( $pkt < 1 OR $user_id == 0)
{
    $message = 'This is a message.';

    echo "<SCRIPT> //not showing me this
       alert('".$message."');
    </SCRIPT>";

    sleep(10);

    mysql_close();

    header("Location: http://dunno.com/file.php");
}

Upvotes: 0

M. kakkar
M. kakkar

Reputation: 1

Use $_SERVER 'php_self'. It will allow you to create script on same page where you want alert. In this way you won't have to add header.

Here's an example:

<form name="login" action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST" accept-charset="utf-8" class="form_class">
                        <ul>
                            <li>
                                <label for="usermail">Username</label>
                                <input type="text" name="username" placeholder="Enter username" required>
                            </li>
                            <li>
                                <label for="password">Password&nbsp;</label>
                                    <input type="password" name="password" placeholder="Enter your password" required>
                            </li>
                            <li id="custom">
                                <input type="submit" value="Sign In">
                            </li>
                        </ul>
                    </form>


$sqluname="root";
$sqlpword="hrhk";
$database="registration";
$server="127.0.0.1";
$db_handle=mysql_connect($server,$sqluname,$sqlpword,$database);
$db_found = mysql_select_db($database, $db_handle);

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $uname = $_POST["username"];
    $pword = $_POST["password"];

    $ulength=strlen($uname);
    $plength=strlen($pword);

        if($ulength > 5){
            $sqlquery="SELECT * FROM members WHERE username='$uname' AND password='$pword'";
            $result=mysql_query($sqlquery);
            $num_rows=mysql_num_rows($result);
                if ($num_rows >0) {
                    echo "<script>alert('Logged In');</script>";

                    session_start();
                        $_SESSION['sid']=$uname;
                        header("location:Yahoo/yahoo.php");
                }
                else{

                    echo "<script>alert('Wrong Username or Password!');</script>";
                }
        }
        else{


            echo"<script>alert('Username should be greater than 5 characters.');</script>";
        }

}

?>

Upvotes: 0

John Carroll
John Carroll

Reputation: 156

Aaroniker is correct with the redirect comment. One way to make it work would be to send the message with a session variable and then add the message creation to the next page like so.

session_start();
if ( $pkt < 1 OR $user_id == 0) {
$_SESSION['message']= "this is a message";
header("Location: http://dunno.com/file.php");

On file.php:

session_start();
echo "<SCRIPT> //not showing me this
alert($_SESSION['message']);
</SCRIPT>";
    mysql_close();
}

Another option would be to pass them with you header. Like so:

if ( $pkt < 1 OR $user_id == 0) {
$message= "this is a message";
header("Location: http://dunno.com/file.php?message=" . $message . ");

Then on file.php:

echo "<SCRIPT> //not showing me this
alert($_GET['message']);
</SCRIPT>";
    mysql_close();
}

Upvotes: 0

bashleigh
bashleigh

Reputation: 9314

Your error is in the structure of your code. PHP, even in OOP is procedural and will run each line one after the other. So the way you've structured your code means the page is redirected before your echo. A better method is to use the javascript alert box to redirect your page using:

var prompt=window.confirm("message here. \r\n"+"Do you want to redirect?");
if(prompt)document.location.href='http://dunno.com/file.php');

Upvotes: 0

Aaroniker
Aaroniker

Reputation: 190

this is a redirect: header("Location: http://dunno.com/file.php");

the code below wont work

Upvotes: 0

Related Questions