I think that there is either an error in my code, or my PHP or Apache is set up incorrectly.
When I submit a form with a hidden field in it, I do not get any data in my $_POST array...
When I comment out the hidden field in my code, the POST data is returned correctly...
HTML FORM
<form action='/utils/login.php ' method='POST'>
<table>
<tr>
<td colspan='2'>
Login
</td>开发者_开发问答
</tr>
<tr>
<td>
Username
</td>
<td>
<input type='text' name='userid' value='' size='12' />
</td>
</tr>
<tr>
<td>
Password
</td>
<td>
<input type='password' name='password' size='12' />
</td>
</tr>
<tr>
<td>
<input type='hidden' name='formtype' value='login' />
</td>
</tr>
<tr>
<td>
<input type='submit' value='Submit' />
</td>
</tr>
</table></form>
Here is the code that is processing it in PHP...
foreach ($_POST as $var => $value) {
echo "$var = $value<br>";
}
I am using PHP 5 and Apache 2.2 on my server.
Any ideas?
EDIT...
I have narrowed it down to this...
$command = $_POST['formtype'];
When I removed the @ sign from my $_POST, I am getting the following error...
Notice: Undefined variable: formtype in C:\webroot\utils\login.php on line 17
If I comment out that line, the POST data is passed into the program without a problem.
I would suggest changing the code you are using to display the contents of $_POST
to a single call:
print_r($_POST);
Anytime you are displaying the entire contents of an array, this is better than a loop w/ echo, as it will show every value at every level of the array.
Also, as was mentioned in a comment, make sure you close the form in the html.
You never closed your <form>
tag.
And I see now that someone beat me to it by a mile in the comments. Still, this is the right answer.
Have you tried taking the hidden input out of the table and placing it right after the opening form tag?
You can also use:
var_dump($_POST);
...to view the post variables.
Also, if any inputs are being dynamically created or might be missing from the POST variables... you can use:
variable = 'default';
if(isset($_Post['variable'])) $variable = $_POST['variable'];
...to dynamically set variables that could be there or not.
I changed my form to work with Twig. The changed form was not sending the hidden input value with post. In case someone has the same problem, I solved it by doing the following.
The original line was:
<input hidden name='foo[{{ loop.index }}][id]' value='{{id}}' />
I sold it by making type='hidden':
<input type='hidden' name='foo[{{ loop.index }}][id]' value='{{id}}' />
Please try with:
<form action="..." method="post" enctype="application/x-www-form-urlencoded">
精彩评论