5

Possible Duplicate:
PHP: “Notice: Undefined variable” and “Notice: Undefined index”

I am just learning PHP and I keep getting an Undefined Index error. The book I'm learning from has an HTML form and a PHP page that processes the form, using the following format:

<!-- The form fields are all set up something like this -->
<input type="text" id="howlong" name="howlong" /><br />

// The PHP starts with one line like this for each of the form fields in the HTML
$how_long = $_POST ['howlong'];

// And there is one line for each one like this to output the form data: 
echo ' and were gone for ' . $how_long . '<br />';

The example I'm working with has about 12 form fields.

What's odd is that not all of the variables throw this error, but I can't see a pattern to it.

I've checked that all HTML fieldnames match up with the PHP $_POST variable name I entered, and I've made certain that when I fill out the form and submit it that all fields are filled in with something. Interestingly, the completed code that can be downloaded for the book also throws this error.

I realize this code may not reflect best practices, it's from the first chapter of the book and obviously I am a noob :)

In case it makes a difference, I am using PHP 5.3.5 on XAMPP 1.7.4 with Windows 7 Home Premium.

Community
  • 1
  • 1
red4d
  • 277
  • 2
  • 4
  • 10

3 Answers3

6

Remember to set the method to POST on the form tag...

heres the code i used to try yours, and it worked to me:

in a file named test.php:

<html>
  <body>
    <form method="POST" action="testProc.php">
      <input type="text" id="howlong" name="howlong" /><br/>
      <input type="submit" value="submit"/>
    </form>
  </body>
</html>

and in testProc.php:

<?php
if (isset($_POST)) {
  if (isset($_POST["howlong"])){
    $howlong = $_POST['howlong'];
    echo ' and were gone for ' . $howlong . '<br />';
  }
}
?>

Just as an advise, to make display manipulation with stylesheets i recommend to put forms within a table, like this:

<html>
  <body>
    <form method="POST" action="testProc.php">
      <table>
        <tbody>
          <tr>
            <th>
              <label for="howlong">How long? :</label>
            </th>
            <td>
              <input type="text" id="howlong" name="howlong" />
            </td>
          </tr>
          <tr>
            <input type="submit" value="submit"/>
          </tr>
        </tbody>
      </table>
    </form>
  </body>
</html>

Hope you can use this...

Throoze
  • 3,988
  • 8
  • 45
  • 67
  • What's the best way to use isset() for a dozen form fields? – red4d May 30 '11 at 23:04
  • Pffft. I let a simple syntax error slip in there three times...used a dollar sign where I shouldn't `$_POST['$howlong']`. If I hadn't run the isset you recommended, I would prolly still be scratching my head. Works now! Thanks! – red4d May 30 '11 at 23:57
  • that happens a lot... i personally hate using dollar sign for variables. About your first comment, the one thing that comes to my mind is having an array with the keys you want to test (lets call it `a = {"name", "passwd", "email", ... };`) and iterate through it calling something like `isset($_POST[$a[i]])` within a `for` loop testing every key at once. Although I've never done that, i always test each key one at a time. However, i think you should keep your forms as short as possible for "user friendlyness" hehehe – Throoze May 31 '11 at 03:19
  • Yeah, I was using NetBeans and had a code template to make the process of inserting those variables from $_POST quicker...only for some reason I added a $ sign to the template where the form field names go, so NetBeans did what I asked and added it :) I ended up checking the fields one at a time as well, just seemed like less of a hassle. Thanks for the help. – red4d Jun 02 '11 at 14:01
2

you need to check that form is submitted and then you can try to use $_POST array, so you should put this code above:

if(isset($_POST['send'])) {

where "send" is name of submit button

Kamil
  • 782
  • 1
  • 6
  • 22
1

You can test to see if a variable is set using the isset() function.

Also, not all HTML form elements will post a value in all cases. The common example is the checkbox; an unchecked checkbox doesn't form part of the the data posted back to the server. Therefore the $_POST element you're expecting to be set won't be.

Matty
  • 33,203
  • 13
  • 65
  • 93