No POST data being returned when hidden input type is present

大城市里の小女人 提交于 2021-01-27 06:14:06

问题


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.


回答1:


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.




回答2:


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.




回答3:


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.




回答4:


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}}' />



回答5:


Please try with:

<form action="..." method="post" enctype="application/x-www-form-urlencoded">


来源:https://stackoverflow.com/questions/3234189/no-post-data-being-returned-when-hidden-input-type-is-present

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!