Javascript 警报和 php 标题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23271812/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:37:05  来源:igfitidea点击:

Javascript alert and php header

php

提问by user3565273

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?

我有一个小问题。当我得到没有标题的 PHP 脚本时,我收到了 javascript 警告框。但是当我在警报之前使用标题时,它不起作用。它像它应该的那样重定向我,但它没有显示任何框。有人可以帮助我吗?

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)

@edit 也许有任何命令可以让警报等待整个浏览器加载?(如果是问题)

采纳答案by Zander Rootman

Maybe try redirect using JavaScript.

也许尝试使用 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();
}

回答by bashleigh

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:

您的错误在于您的代码结构。PHP,即使在 OOP 中也是程序性的,并且会一个接一个地运行每一行。因此,您构建代码的方式意味着页面在您的回声之前被重定向。更好的方法是使用 javascript 警报框重定向您的页面:

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

回答by John Carroll

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.

Aaroniker 的重定向评论是正确的。使其工作的一种方法是使用会话变量发送消息,然后像这样将消息创建添加到下一页。

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:

在 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:

然后在 file.php 上:

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

回答by M. kakkar

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.

使用 $_SERVER 'php_self'。它将允许您在需要警报的同一页面上创建脚本。这样您就不必添加标题。

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>";
        }

}

}

?>

?>

回答by saltcracker

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.

我知道这是一篇旧帖子,但这是我在类似示例中的制作方法。此示例检查管理员是否已从特定 IP 地址登录,如果返回 false,则应将用户重定向回登录页面并显示错误消息。

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:

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

回答by Aaroniker

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

这是一个重定向: header("Location: http://dunno.com/file.php");

the code below wont work

下面的代码不起作用

回答by llanato

The below header function will run a 302 redirect by default and redirect the client to http://dunno.com/file.phpso 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.

下面的头函数将默认运行 302 重定向并将客户端重定向到http://dunno.com/file.php,因此在头函数之后开始处理代码之后的任何代码都不会被看到,因为客户端是从此代码所在的页面。

 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

请在此处进一步阅读标题功能: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:

如果您需要像这样显示消息,您可以在重定向之前让您编程睡眠/等待几秒钟(在此示例中假设为 10 秒):

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");
}