9

I set up this test page up on my server. Please tell me why the $_POST array does not contain anything even when I submit the form. I have tried this in three different browsers and nothing happens.

<?php print_r($_POST);?>

<form method="post">

<p><label>Email: </label>
<input type="text" id="login_email" />
</p>

<p><label>Password: </label>
<input type="password" id="login_password" />
</p>

<p><label>Remember Me?: </label>
<input type="checkbox" id="login_remember" />
</p>

<p>
<input type="submit" value="Login" />
</p>

</form>

I have been writing PHP for years and this has never happened before. What is wrong with this code?

user2864740
  • 60,010
  • 15
  • 145
  • 220
Mike Stanford
  • 404
  • 1
  • 5
  • 8

7 Answers7

52

Your input elements do not have name attributes. Should be:

<input type="text" id="login_email" name="login_email" />

If an input element does not have a name attribute, it is not sent as part of the POST data.

Ayman Hourieh
  • 132,184
  • 23
  • 144
  • 116
9

Well, you don't have an action for the form tag? It should be the script name:

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

...and you're also not setting the names for each form input - the browser doesn't submit the ID as the element name.

BrynJ
  • 8,322
  • 14
  • 65
  • 89
  • Actually, I think it is valid to leave the action name out. If you do, the browser just posts/gets to the current page. – Samantha Branham May 12 '09 at 18:43
  • 6
    Most browsers do exhibit this behaviour, but according to official specs, the action attribute is required - http://www.w3.org/TR/html401/interact/forms.html. – BrynJ May 12 '09 at 18:46
  • Thanks for the clarification. I agree that it is a bad idea to express that kind of ambiguity when the spec doesn't define a default behavior. – Samantha Branham May 12 '09 at 18:52
4
<form method="POST" action="<?php echo $PHP_SELF; ?>

<p><label>Email: </label>
<input type="text" name="login_email" />
</p>

<p><label>Password: </label>
<input type="password" name="login_password" />
</p>

<p><label>Remember Me?: </label>
<input type="checkbox" name="login_remember" />
</p>

<p>
<input type="submit" value="Login" />
</p>

</form>
Kladskull
  • 10,332
  • 20
  • 69
  • 111
2

There's no name attribute for the input elements.

cgp
  • 41,026
  • 12
  • 101
  • 131
1

I suggest you write something like the follow functions based on the Zend_View helpers.

formText($name, $value = null, array $attribs = null)
formPassword($name, $value = null, array $attribs = null)
formLabel($id, $text, array $attribs = null)
formHidden($name, $value = null, array $attribs = null)
formSubmit($name = null, $text = null, array $attribs = null)
formSelect($name, $selected, array $attribs = null, array $options = null)
formCheckbox($name, $default, array $attribs = null, array $options = null)

Then you will never forget/miss something like this again.

<form method="POST" action="<?php echo $PHP_SELF; ?>

<p>
<?php
echo formLabel('login_email', 'Email'), ':',
     formText('login_email'); 
?>
</p>

<p>
<?php
echo formLabel('login_password', 'Password'), ':',
     formPassword('login_password'); 
?>
</p>

<p>
<?php
echo formCheckbox('login_remember'), ' ', 
     formLabel('login_remember', 'Remember me');
?>
</p>

<p>
<?php
echo formSubmit(null, 'Login');
?>
</p>
</form>

Tip:

  • If id not defined in attribs, id is the same as name, except for labels where id is used in the for="$id" attribute and formHidden should not have a default id either.
  • formCheckbox writes a formHidden by same name before itself with the negative value, so you get a return value if the checkbox is not checked as well.
  • formCheckbox options is an array with the values for checked or unchecked.
  • Use a filter with FILTER_VALIDATE_BOOLEAN to read the return value from a checkbox to check if it was marked or not.
OIS
  • 9,833
  • 3
  • 32
  • 41
0

All of your input elements need a name attribute.

Jon Ursenbach
  • 2,952
  • 5
  • 20
  • 22
0

You forgot the name attributes for making your script work. You also can include the "for" tag in your labels to match your input's names attributes. This isn't a requirement but can help with CSS formating of your form:

<p>
<label for="login_email">Email: </label>
<input type="text" name="login_email" id="login_email" />
</p>

Helps match everything of and keep your code more streamline and readable if you have to come back to it 6 months later. The action attribute if you aren't going to populate one I would include this as your action:

<form method="POST" action="<?php echo $PHP_SELF; ?>

This will make sure your page is good as far as the form's requirements as well as do as your script should execute. Seems like a simple over-sight. Hope this helps.

stogdilla
  • 232
  • 2
  • 14