No POST data being returned when hidden input type is present
Asked Answered
H

5

5

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.

Hornstone answered 13/7, 2010 at 4:26 Comment(6)
Did you close your <form>? I don't see the closing tag.Tunis
You don't need annother table cell for the hidden feild...Mcgill
Yes. My page had the closing </form> tag within it. I had forgotten to paste it into the SO posting. That is not the problem with the form.Hornstone
Update - I tried turning of E_Notice errors, but that did not work. The problem occurs when I try to assign or access the POST variables.Hornstone
you could try changing it to a normal field, and changing the name of the field too and see if that works. might help narrow it downAllomorphism
Works fine if the input is text and hidden with jQuery: $('#pid').hide(); $('#itemType').hide();Womb
M
2

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.

Monarda answered 13/7, 2010 at 5:13 Comment(1)
Thanks. I don't think that is the reason why the page is not returning any post data. Remember that when I comment out the <input type="hidden"> element entirely, POST back data is returned as expected.Hornstone
K
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.

Kinaesthesia answered 13/7, 2010 at 5:15 Comment(1)
I can see the form tag has been closed. Is there any wrong in it. I'm getting same problem so read your answer.Coy
K
2

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.

Kickoff answered 4/8, 2011 at 14:33 Comment(1)
Putting the hidden inputs directly after the form tag fixed a similar problem that I was having.. thanks!Siffre
R
1

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}}' />
Rustication answered 3/5, 2016 at 9:15 Comment(0)
S
0

Please try with:

<form action="..." method="post" enctype="application/x-www-form-urlencoded">
Skyscape answered 14/10, 2014 at 4:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.