Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No POST data being returned when hidden input type is present

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.

like image 996
Grizzly Peak Software Avatar asked Jul 13 '10 04:07

Grizzly Peak Software


People also ask

What happen when we define input type hidden?

The <input type="hidden"> defines a hidden input field. A hidden field lets web developers include data that cannot be seen or modified by users when a form is submitted. A hidden field often stores what database record that needs to be updated when the form is submitted.

Are hidden inputs submitted?

Even though the hidden input cannot be seen at all, its data is still submitted.

How do you pass an input hidden array?

If you want to post an array you must use another notation: foreach ($postvalue as $value){ <input type="hidden" name="result[]" value="$value."> }


2 Answers

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.

like image 191
Jeffrey Blake Avatar answered Nov 15 '22 11:11

Jeffrey Blake


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.

like image 32
Dagg Nabbit Avatar answered Nov 15 '22 13:11

Dagg Nabbit