【问题标题】:How can I display a <script> alert('Fill Out Information')</script> When Using header() function php如何在使用 header() 函数 php 时显示 <script> alert('Fill Out Information')</script>
【发布时间】:2020-07-16 08:41:03
【问题描述】:

我要做的是使用php创建一个创建帐户系统我的问题是在我检查之后

提交按钮如果一个或所有输入为空,用户将发送相同的页面而不是主页

我的问题是将用户发送到 signup.php 的 header() 函数关闭了

echo "alert('Hello')";他们是我可以发送警报消息的一种方式,以便我的用户

可以知道他们犯了什么错误我尝试先写错误消息,但仍然出现同样的问题。

<?php
    if(isset($_POST['signup-submit'])){

        //my database connection    

        require "dbh.inc.php";

        $username=$_POST['uid'];

        $email=$_POST['mail'];

        $password=$_POST['pwd'];

        $passwordRepeat=$_POST['pwd-repeat']

        if(empty($username)||empty($email)||empty($password)||empty($passwordRepeat)){

            header("Location: ../signup.php? Fill out the  information");

            echo "<script>alert('Hello')</script>";

        }
    }
?>

【问题讨论】:

  • 如果用户未能正确填写表单,您可以设置会话变量,重定向到同一页面并检查会话变量 - 如果存在则显示 javascript 警报

标签: php validation input header login-script


【解决方案1】:

如果您要使用会话变量(它将在页面重新加载时持续存在),您可以使用简单的逻辑测试来确定是否设置此变量并检查页面何时重新加载(如果存在)以确定是否显示 javascript 弹出窗口 显示警报后,删除变量。

<?php

    session_start();

    if( $_SERVER['REQUEST_METHOD']=='POST' ){
        if( isset( $_POST['uid'], $_POST['mail'], $_POST['pwd'], $_POST['pwd-repeat'] ) ){

            /* ok */
            require "dbh.inc.php";
            $username=$_POST['uid'];
            $email=$_POST['mail'];
            $password=$_POST['pwd'];
            $passwordRepeat=$_POST['pwd-repeat'];


        }else{
            $_SESSION['error']=true;
            exit( header('Location: ../signup.php?error=true') );
        }
    }
?>
<html>
    <head>
        <title></title>
        <?php
            if( !empty( $_SESSION['error'] ) ){
                echo "
                <script>alert('Please fill out all fields')</script>";

                unset( $_SESSION['error'] );
            }
        ?>
    </head>
    <body>
        <!-- html contents -->
    </body>
</html>

【讨论】:

    猜你喜欢
    • 2019-02-22
    • 2016-10-13
    • 1970-01-01
    • 2013-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-25
    • 2022-12-02
    相关资源
    最近更新 更多