tags:

views:

1578

answers:

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?

+7  A: 

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
Actually, I think it is valid to leave the action name out. If you do, the browser just posts/gets to the current page.
Stuart Branham
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
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.
Stuart Branham
+29  A: 

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
+2  A: 

There's no name attribute for the input elements.

altCognito
+3  A: 
<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>
Mike Curry
This will work... you forgot the 'name' and action parts.
Mike Curry
A: 

Wow... im speechless. i cant believe i forgot something as simple as the name attribute :S i need a break from the computer for forgetting that lol. Thanks alot Ayman, it works now.

@BrynJ, if you dont include an action attribute, it defaults to the running script.

Mike Stanford
Comments to answers go on those answers, not as new answers.
Welbog
For future reference, these kind of replies shouldn't be answers, but instead comments or edits on the original question. This is why you were downvoted.
Stuart Branham
Ahh ok, im new to the site so i didnt know.
Mike Stanford
@Mike Stanford - most browsers do exhibit this behaviour when omitting the action attribute, but according to official specs this attribute is required - http://www.w3.org/TR/html401/interact/forms.html
BrynJ
Fair enough, i only omit them when the site is in pre production anyway. Usually because i am using messy html code which will be rewritten.
Mike Stanford
A: 

All of your input elements need a name attribute.

Jon Ursenbach
+1  A: 

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
A: 

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
A: 

Thanks a lot guys. Adding the name attribute solved my problems too. Husein Poonawala.

Husein Poonawala