0

Do I have to declare

exit ();

when I using

header ("Refresh:1");

Link provided stated about location. Yet my question is about refresh. Do I have to exit() everytime I refresh?

I want to refresh the msg box after 1 second it appear when certain condition is triggered. But after I exit() for every refresh I have, my page become plain white for 1 seconds. What I want is just that msg box to be refreshed

My code:

<?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>
Incognitorrrr
  • 65
  • 1
  • 1
  • 11
  • [Why I have to call 'exit' after redirection through header('Location..') in PHP?](https://stackoverflow.com/q/2747791/6521116) – LF00 May 25 '17 at 04:24
  • @KrisRoofe refer my new edited question – Incognitorrrr May 25 '17 at 04:35
  • You needn't do the exit(), for you just want to refresh the page. Refer to [Refresh a page using php](https://stackoverflow.com/q/12383371/6521116) – LF00 May 25 '17 at 04:58
  • I think a more proper way is that you needn't to refresh the whole page, you can use js to refresh the page content. – LF00 May 25 '17 at 05:05

0 Answers0