我第一次打开页面时没有设置

I'm still new and trying to learn php. I have a form and everytime I run it I get an error displaying that the variable were not set when they should be. I'm definately missing something. Kindly explain what why is the variable $_POST['login_button'] not set the first time i run the page?

Code can be found below:

<?php

require 'connect.inc.php';

    if (isset($_POST['login_button']) && isset($_POST['username']) && isset($_POST['password'])){

        $login_button = $_POST['login_button'];             
        $username = $_POST['username']          ;
        $password = $_POST['password'];         
        $password_hash = md5($_POST['password']);           

            if(!empty($username)&&!empty($password)){                   
            $sql = "SELECT `id` FROM `golden_acres_username` WHERE `uname`='$username' AND '".$password_hash."'";               
                if($sql_run = mysql_query($sql)){           
                    $query_num_rows = mysql_num_rows($sql_run);
                }       
                    if($query_num_rows==0){     
                        echo'User name and password are incorrect';     
                        }               
                    else if($query_num_rows==1)
                        {
                        echo 'Username and password are correct';
                        }
                    }   

            else                                                
            {                       
            echo 'Please fill in user name and password';                       
            }   
    }       
    else
    {                           
    echo'Fields are not set';                           
    }                           
?>                                      
<form class="home_logon_area" action="test.php" method="POST">
    Username: 
    <input type="text" name="username" />
    Password:
    <input type="password" type="password" name="password"/>
    <input type="submit" name="login_button">
</form>

Thanks in advance,

Joseph

The $_POST variable is set by the server to capture the data content sent by the browser as part of the form POST action. When the page is initially loaded, the browser has only executed/requested a GET call for the content of the page without sending the POST request.

Hope that helps!

$_POST contains the result of submitting a form. If no form has been submitted yet, it will not contain anything.

Your script is working just fine; remove echo 'Fields are not set';, or use that line for code that should only run when the form hasn't been submitted yet.

This is simple to understand ;-)

  • First time the phpscript is executed to get the Form

    So there will be no information at all (the visitor is new and have not seen the form before)

  • Then the User fills the form and press Submit button

    The form is linked to the same side so the same phpscript gets executed again

  • Now you have the Formular values transmitted and you can acess them over $_POST

For more information look at php.net

Remove last else from your code and update the form with this one

<form class="home_logon_area" action="test.php" method="POST">
    Username: 
    <input type="text" name="username" required />
    Password:
    <input type="password" type="password" name="password" required/>
    <input type="submit" name="login_button">
</form>