0

I'm building a form to be used to input data into a database. I have the basic form and validation built (I think). When I try testing it on my server/domain I keep getting the error undefined Index: Form Submit. I'm certain it's something minor, but I can't find my error. An extra set of eyes looking at this would be greatly appreciated.

<!DOCTYPE HTML>
<html lang = "en">
<head>
<link rel="stylesheet" type="text/css" href="mystylesheet.css">
<title>Tech Order Department.html</title>
<meta charset = "UTF-8" />

</head>

<body>

<!--Designing my form-->

<h2>New Projects</h2>

<br>

<?php
  if($_POST['formSubmit'] == "Submit") 
  {
    $varMovie = $_POST['formProject'];
$varMovie = $_POST['formClient'];
$varMovie = $_POST['formLastName'];
$varMovie = $_POST['formDateReceived'];
  }
?>


<form action="myform.php" method="post">

       Project:<br>
       <input type="text" name="Project">
<br>
       Client:<br>
       <input type="text" name="Client">

<br>
       Last Name:<br>
       <input type="text" name="LastName">

<br>
       Date Received:<br>
       <input type="text" name="DateReceived">


  <br><br>

    <input type="submit" name="formSubmit" value="Submit">

</form>   

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

  if(empty($_POST['formProject'])) 
  {
    $errorMessage .= "<li>You forgot to enter a project name!</li>";
  }
  if(empty($_POST['formClient'])) 
  {
    $errorMessage .= "<li>You forgot to enter a client name!</li>";
  }
  if(empty($_POST['formLastName'])) 
  {
    $errorMessage .= "<li>You forgot to enter the tech writer name!</li>";
  }
  if(empty($_POST['formDateReceived'])) 
  {
    $errorMessage .= "<li>You forgot to enter the date received!</li>";
  }


  $varMovie = $_POST['formProject'];
  $varName = $_POST['formClient'];
  $varMovie = $_POST['formLastName'];
  $varMovie = $_POST['formDateReceived'];

  if(!empty($errorMessage)) 
  {
    echo("<p>There was an error with your form:</p>\n");
    echo("<ul>" . $errorMessage . "</ul>\n");
  } 

}

?>


</body>

</html>
Tony
  • 298
  • 3
  • 17

3 Answers3

2

You should change :

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

To :

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

When you're using form, you should ALWAYS verify at least once posted that your fields are set.

For example :

We have this quick form :

<form action="action_page.php" method="POST">
  First name:<br>
  <input type="text" name="firstname" value="">
  <br>
  Last name:<br>
  <input type="text" name="lastname" value="">
  <br><br>
  <input type="submit" value="Submit">
</form>

First note that on the I've put two attributes :

  • action : Which is where the form will be send
  • method : Which is how the form will be send

There are two inputs "firstname","lastname" and a button submit.

Let's say that on the "action_page.php" you want to display : "Hello, firstname lastname".

You could just do the following :

<?php 
   echo 'Hello, '.$_POST['firstname'].' '.$_POST['lastname'];
?>

And that will print out what you want. But in the case where those fields would be empty that would create "Undefined Index"...

So to be sure that you won't be using variables that doesn't exist you should do the following :

<?php 

  //First we create an array that will errors if they occurs
  $error = array();

  //Then we test if firstname exist
  if(isset($_POST['firstname'])) {
     //if it does we can assign him.
     $firstname = $_POST['firstname'];
  }else{
     //if it doesn't that's an error.
     $error[] = 'Please enter your firstname';
  }

  //Then we test if lastname exist
  if (isset($_POST['lastname'])) {
     //if it does we can assign him.
     $lastname = $_POST['lastname'];
  }else{
     //if it doesn't that's an error.
     $error[] = 'Please enter your lastname';
  }

  //In the end we check if we have any error stocked:
  if(empty($error)) {
     //if it's empty we have all our required data to display our echo
   echo 'Hello, '.$firstname.' '.$lastname;
  }else{
     //else let's show the error
     print_r($error);
  }
?>

This way we handle any possible "Undefined Index" before they occur and we are sure that our data will return something or at least have an error related.

Nirnae
  • 1,315
  • 11
  • 23
  • I tried your solution. It cleared the error I was having, but now it's throwing the same errors in the variables I've selected, i.e. $varMovie = $_POST['formProject']; – Tony Nov 06 '15 at 13:22
  • Before using a $_POST var, you should always check if this var is set or not empty otherwhile might create a bunch of undefined index – Nirnae Nov 06 '15 at 13:24
  • This is my first time using forms so I'm still learning some of this. How can I check these variables? – Tony Nov 06 '15 at 14:14
  • You can use if(isset($_POST['something'])) before actually using it, just give me a sec i'll edit my post to give you a better example – Nirnae Nov 06 '15 at 14:32
  • Post updated let me know if anything is unclear – Nirnae Nov 06 '15 at 14:55
  • no sweat. I appreciate the help. I'm looking at the array now and when I load the page, I see this: Array ( [0] => Please enter your firstname [1] => Please enter your lastname ) It's in the error checking portion of the array. Not sure why it's called out like that. BTW, that's the basic form I started with haha. – Tony Nov 06 '15 at 15:11
  • If your fields are filled and you're receiving error, that's wierd can you please put a var_dump($_POST); on the first line of this code and tell me what you're receiving ? – Nirnae Nov 06 '15 at 15:17
  • Actually I'm getting that prior to entering any data. That's what I'm puzzled by it. – Tony Nov 06 '15 at 15:21
  • If you're not entering any data, that's the intended behavior, since you've no data in your $_POST, firstname and lastname are undefined which is why you're receiving an error, fill the inputs, submit your form and it should be fine – Nirnae Nov 06 '15 at 15:24
  • Shouldn't the error check take place after I hit submit though? – Tony Nov 06 '15 at 15:26
  • I bet your form and your error check are on the same file, right ? – Nirnae Nov 06 '15 at 15:28
  • Yes they are. All the examples I've researched had it set up that way, however, none of them were in array's though. – Tony Nov 06 '15 at 15:31
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/94440/discussion-between-nirnae-and-tony). – Nirnae Nov 06 '15 at 15:36
1

For check if there was a POST action :-

if (!empty($_POST))

or

if ($_SERVER['REQUEST_METHOD'] == 'POST')
Abhishek Sharma
  • 6,689
  • 1
  • 14
  • 20
0

Updated your code , check this

<!DOCTYPE HTML>
<html lang = "en">
    <head>
        <link rel="stylesheet" type="text/css" href="mystylesheet.css">
        <title>Tech Order Department.html</title>
        <meta charset = "UTF-8" />
    </head>
    <body>
        <h2>New Projects</h2>
        <?php
        if ($_POST['formSubmit'] == "Submit") {
            $varMovie = $_POST['formProject'];
            $varMovie = $_POST['formClient'];
            $varMovie = $_POST['formLastName'];
            $varMovie = $_POST['formDateReceived'];
        }
        ?>
        <form action="myform.php" method="post">
            Project:<br>
            <input type="text" name="Project">
            <br>
            Client:<br>
            <input type="text" name="Client">
            <br>
            Last Name:<br>
            <input type="text" name="LastName">
            <br>
            Date Received:<br>
            <input type="text" name="DateReceived">
            <br><br>
            <input type="submit" name="formSubmit" value="Submit">
        </form>   
        <?php
        
       if(isset($_POST['formSubmit']) && $_POST['formSubmit'] == "Submit") {
            $errorMessage = "";
            if (empty($_POST['formProject'])) {
                $errorMessage .= "<li>You forgot to enter a project name!</li>";
            }
            if (empty($_POST['formClient'])) {
                $errorMessage .= "<li>You forgot to enter a client name!</li>";
            }
            if (empty($_POST['formLastName'])) {
                $errorMessage .= "<li>You forgot to enter the tech writer name!</li>";
            }
            if (empty($_POST['formDateReceived'])) {
                $errorMessage .= "<li>You forgot to enter the date received!</li>";
            }
            $varMovie = $_POST['formProject'];
            $varName = $_POST['formClient'];
            $varMovie = $_POST['formLastName'];
            $varMovie = $_POST['formDateReceived'];
            if (!empty($errorMessage)) {
                echo("<p>There was an error with your form:</p>\n");
                echo("<ul>" . $errorMessage . "</ul>\n");
            }
        }
        ?>
    </body>
</html>
Ananta Prasad
  • 3,655
  • 3
  • 23
  • 35
  • 1
    @Ananata Prasad Loda I tried your code but I'm getting Undefined index: formSubmit here: if ($_POST['formSubmit'] == "Submit") { – Tony Nov 06 '15 at 13:33