Alper
Alper

Reputation: 1

How do I clear the page content after a form is submitted?

I currently have this code in my project:

require_once('mysql.inc.php');
session_start();

if (!isset($_SESSION['username']) && !isset($_SESSION['uid']))
{
    login_sequence();
}
else
{
    login_check($_SESSION['username'], $_SESSION['uid']);
}

function login_sequence()
{
    echo '<p>' . $pretext . '</p><form method="post" action="" /><label for="password">Password: </label><input type="password" name="password" id="password" /><input type="submit" value="Log In" /><input type="hidden" name="submitted" /></form>';
    if (isset($_POST['submitted']))
    {
        $pword = hash('sha256', $_POST['password']);
        $query = "SELECT pword FROM users WHERE user = 'guest'";
        $result = mysql_query($query);
        $return = mysql_fetch_assoc($result);
        if ($return['pword'] == $pword)
        {
            pageout();
        }
        else
        {
            echo 'You entered the wrong password, if you are not a member, please leave.';
        }
    }
}

function login_check($username, $uid)
{
}

function pageout()
{
    echo('
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" 
    "http://www.w3.org/TR/html4/strict.dtd">

    <html lang="en-US">
        <head>
            <title></title>
            <link rel="stylesheet" type="text/css" href="" />
        </head>
        <body>
            <div id="header">
            <p>WOOT</P>
            </div>
            <div id="">
            </div>
            <div id="navigation">
            </div>
            <div id="footer">
            </div>
        </body>
    </html>
    ');
}

?>

There is a single "Guest" password stored in a database, it accesses the database and checks the password entered by the user against the one stored in the database. I want to have it so after the form is submitted and the information is correct, on the same page the form disappears and the page appears. How do I do it? How do I get rid of the form to make room for the new page?

Upvotes: 0

Views: 1787

Answers (3)

Harmiih
Harmiih

Reputation: 81

I would make structure look slightly different:

//First
if (!isset($_POST['submitted'])) {
    //include your form only
} else {
    //Check if user submitted correct password

   //If password is correct
   if ($return['pword'] == $pword) {
       pageout();
       //OR write pageout code on different page and call header("Location: Yourpage.php");
   } else {
       //Include form or inform user about password mismatch
   }  
}

edit. Also if you use header("Location:") function make sure you're not echoing or including anything that outputs data. This will cause Headers already sent error.

Upvotes: 0

user557846
user557846

Reputation:

header(location ...) on success, or a page display conditional on log in settings

Upvotes: 1

Eli Y
Eli Y

Reputation: 907

simply do

at the end of your form and at the php code do this

    if(!isset($_POST['s'])){

?>
your form here

<?php
}
else //form handle

Upvotes: 0

Related Questions