0

this is my page when i run it i got the excption which sayes that the formSubmit is not define . i know that i have to check if the post contents that variable but i couldn't know the solution

 <?php
    if($_POST['formSubmit'] == "Submit")
    {
        $errorMessage = "";

        if(empty($_POST['formPassword']))
        {
            $errorMessage .= "<li> You forgot to enter your password!</li>";
        }
        if(empty($_POST['formName']))
        {
            $errorMessage .= "<li> You forgot to enter a name!</li>";
        }

        $varPassword = $_POST['formPassword'];
        $varName = $_POST['formName'];

        if(empty($errorMessage)) 
        {
            require('Document1.php');
            User::add_user($varName, $varPassword);
            exit;
            //header("Location: normal.php");
        }


    }
    ?>

    <html>
       <head>

        <title>SIGN UP</title>

        <style type="text/css">
    <!--
    .style1 {
        font-size: xx-large;
        color: #0000CC;
    }
    -->
        </style>
    </head> 

     <body>
     <p align="center"><br>
       <span class="style1">SIGN UP</span></p>
     <p>&nbsp;</p>
     <form action="myform1.php" method="post">

            <p>
                What is Your NAME ?<br>
            <input type="text" name="formName" maxlength="50" value="" />               
            </p>

            <p>
                What is Your PASSWORD ?<br>
                <input type="text" name="formPassword" maxlength="50" value="" />
            </p>

            <?php
            /*
            ?>
            <p>
                What is Your Email ?<br>
                <input type="text" name="formEmail" maxlength="50" value="" />
            </p>
            */
                ?>          
            <input type="submit" name="formSubmit" value="Submit" />

            <input type=reset value = "delete fields ">

     </form>
     </body>
    </html>

any help would be appreicated

Rand
  • 13
  • 1
  • 3
  • It is because the first time the page loads, you have not yet populated `$_POST`. See [this reference question](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) for info on how to avoid the E_NOTICE prior to actually posting the form. – Michael Berkowski Jul 14 '13 at 17:23

6 Answers6

4

The reason is because the PHP code also runs the very first time when the page loads, even though the form isn't yet submitted. The solution is to change your first IF statement as follows:

<?php
if(isset($_POST['formSubmit']) && $_POST['formSubmit'] == "Submit")
{
// OTHER CODE REMAINS AS IT IS
}
?>

It will now check that not only $_POST['formSubmit'] is set to "Submit" but also that it exists. When the page first loads, $_POST['formSubmit'] will not be available and therefore no exception will be thrown.

Hope it helps!

Abhay
  • 6,545
  • 2
  • 22
  • 17
1

You can check that its a $_POST request with $_SERVER['REQUEST_METHOD'] === 'POST' also add errors to an array this way you can place the errors where you want.

<?php
if($_SERVER['REQUEST_METHOD'] === 'POST'){
    $error = array();

    if(empty($_POST['formPassword']))
    {
        $error['pass'] = "You forgot to enter your password!";
    }
    if(empty($_POST['formName']))
    {
        $error['name'] = "You forgot to enter a name!";
    }

    if(empty($error))
    {
        require('Document1.php');
        User::add_user($_POST['formName'], $_POST['formPassword']);
        exit;
        //header("Location: normal.php");
    }
}
?>

Then if error just add <?php echo isset($error['name']) ? $error['name'] : null ?> where you want the message specific to the error. like next to the input for name.

Lawrence Cherone
  • 46,049
  • 7
  • 62
  • 106
1

I suggest using .

if (isset($_POST['formSubmit']) && ($_POST['formSubmit'] == "Submit")) {
    /* ... */
}

isset() method will check if value exists

user1759572
  • 683
  • 4
  • 11
0

It's always a good practice to use isset() to check if a variable is set.

Change

if($_POST['formSubmit'] == "Submit")

to

if(isset($_POST['formSubmit']) && $_POST['formSubmit'] == "Submit"){
//rest of the code
}

See also: empty()

0

Before you try to get the value of $_POST['formSubmit'], you should check if it is set with isset($_POST['formSubmit'])

if (isset($_POST['formSubmit']) && $_POST['formSubmit'] == "Submit") {
    // your code
}

I think it also work with:

if ($_POST['formSubmit'] && $_POST['formSubmit'] == "Submit") {
    // your code
}

but is better to use isset()

Miawa
  • 282
  • 1
  • 4
  • 10
0

Rather than using isset() everywhere, I prefer to declare default values. For example...

$defaults = array(
    'formSubmit' => '',
    'formPassword' => '',
    'formName' => '',
);

$form = $_POST + $defaults;

if ($form['formSubmit'] == 'Submit') {
    // ...
Rob
  • 12,659
  • 4
  • 39
  • 56