标题(刷新)是否需要退出()?

时间:2017-05-25 04:19:57

标签: php

我必须声明

  

exit();

当我使用

  

标题(“刷新:1”);

链接提供了有关位置的说明。但我的问题是关于刷新。我每刷新一次都要退出吗?

我希望在触发某些条件后1秒钟后刷新msg框。但是在我每次刷新后退出()后,我的页面变为纯白色1秒钟。我想要的只是刷新msg框

我的代码:

<?php
session_start();
$username = $_SESSION['username'];
if(!isset($_SESSION['username']))
    {
        header("location:login.php");
    }

     $host = 'localhost';
     $user = 'root';
     $pass = '';
     $name = 'dbname';
     $msg =  $msg2 ='';//display info if error or not

     $username = $password = $tel = $error ='';
    if($_SERVER['REQUEST_METHOD'] == 'POST')
    {
        //connecting
        $con = new mysqli($host,$user,$pass,$name);
        if ($con->connect_error)
            die("Connection failed ".$con->connect_error);


        //REGISTER OPTION------------------------------------------------
        if(isset($_POST['register']))
        {
            $username = $con->real_escape_string($_POST['username']);
            $password = $con->real_escape_string($_POST['password']);
            $email = $con->real_escape_string($_POST['email']);
            $hash = password_hash($password,PASSWORD_DEFAULT); 
            $tel = $_POST['tel'];

            //Check if username if exist?
            $data2= 
            "
                SELECT * FROM users WHERE username ='".$username."'
            ";

            $result=$con->query($data2);
            $rows = mysqli_num_rows($result);

            if($rows != 0)
            {
                $msg = "Username already exist";
                header ("Refresh:1");
                exit();
            }
            else
            {
                //Check if email exist?
                if (filter_var($email, FILTER_VALIDATE_EMAIL))
                {
                    $data2= 
                    "
                        SELECT * FROM users WHERE email ='".$email."'
                    ";

                    $result=$con->query($data2);
                    $rows = mysqli_num_rows($result);

                    if($rows != 0)
                    {
                        $msg = "Email already exist";
                        header ("Refresh:1");
                        exit();
                    }

                    else
                    {
                        //Insert values
                        $data = 
                        "
                            INSERT INTO users (username,password,email,tel) VALUES ('$username','$hash','$email','$tel')
                        ";

                        if( $con->query($data) === true)
                        {
                            $msg = "Successfully registered";
                            header("Refresh:1");
                            exit();
                        }
                    }
                }
                else
                {
                    $msg = "Invalid email format";
                }
            }   
        }

        //-----------------------------------------------------------------

        //DELETE OPTION------------------------------------------------
        if (isset($_POST['delete']))
        {
                $username = $con->real_escape_string($_POST['username']);

                /*Check if username exist?*/
                $data2= 
                "
                    SELECT * FROM users WHERE username ='".$username."'
                ";
                $result=$con->query($data2);
                $rows = mysqli_num_rows($result);

                if($rows != 0)
                {
                    /*Deleting process*/
                    $data = 
                    "
                        DELETE FROM users WHERE username = '".$username."'
                    ";
                    $result2 = $con->query($data);
                    if($con->query($data) === true)
                        $msg2 = "Success";
                        header("Refresh:1");

                }

                else
                {
                        $msg2 = "Username doesn't exist";
                        header("Refresh:1"); //refresh page 1 saat
                }
        }
        //----------------------------------------------------------------
    }
?>
<html>
<title>.:Admin:.</title>
        <h2 align="center">Register</h2>
<body bgcolor="cyan">   
    <center>
    <form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>" method='POST'>
    <fieldset style="width:15px;height:150px;background-color:#DAF7A6;border:0;">
    <table align="center" border="0" name="register">
        <tr>
            <td>Username:
            <td><input type="text" name="username" required>
        </tr>

        <tr>
            <td>Email:
            <td><input type="text" name="email" required>
        </tr>

        <tr>
            <td>Password:
            <td><input type="password" name="password" required>
        </tr>

        <tr>
            <td>Phone Number:
            <td><input type="text" name="tel">
        </tr>

        <tr>
            <td colspan="2" align="center"><?php echo $msg;?>
        </tr>

        <tr>
            <td colspan="2"><button type="submit" name="register">Register</button>
        </tr>

    </table>
    </fieldset>
    </form>
    </center>

    <br><br>


    <h2 align="center">Delete User</h2>
    <form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>" method='POST'>
    <table align="center" border="0" name="delete">

    <tr>
        <td>Username:
        <td><input type="text" name="username" required>
    </tr>

    <tr>
        <td colspan="2" align="center"><?php echo $msg2;?>
    </tr>

    <tr>
        <td colspan="2"><button type="submit" name="delete"> Delete User</button>
    </tr>
    </table>
    </form>
    </body>
</html>

0 个答案:

没有答案