Known problems with hidden fields and forms?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
MattSharp
Forum Commoner
Posts: 62
Joined: Wed Apr 24, 2002 2:25 pm

Known problems with hidden fields and forms?

Post by MattSharp »

I have a form that only contains one field and its hidden. This is used so that we can use submit buttons to select which option the person wants. I realize there are more practical ways to do this but I am reconfiguring a script and eventuall will re-do it better, but for the time being this is how it is done. The script worked previously, but after recently upgrading to a newer version of PHP I have run into a rather odd problem. When I pass one hidden field in via POST and output it on the next name it shows up in the form as name=value (of course it should just be value). If I add a second hidden field to the form and run it, everything works fine. Has anyone heard of or seen this problem before? Is there anything I can do/try short of adding a dummy hidden field to each form this is used in?
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Could we see the form code and the bit of code you use to display it on the next page?

Mac
hedge
Forum Contributor
Posts: 234
Joined: Fri Aug 30, 2002 10:19 am
Location: Calgary, AB, Canada

Post by hedge »

I have hade some strange things like that, usually comes back to having slight errors in the html. Missing quotes or end tags have been my nemesis.
MattSharp
Forum Commoner
Posts: 62
Joined: Wed Apr 24, 2002 2:25 pm

Post by MattSharp »

twigletmac wrote:Could we see the form code and the bit of code you use to display it on the next page?

Mac
Form Code:

Code: Select all

<form method="POST" action = "options.php">
                <input type="hidden" name="machine" value="thisone" />
                <input type="submit" value="Select" />&nbsp;&nbsp;&nbsp;This one (This one)
        </form>
Display

Code: Select all

<font size="2" color="black">Test: <?=$machine?></font><br />
As I said if I add a second hidden field to the the form then both display properly.
MattSharp
Forum Commoner
Posts: 62
Joined: Wed Apr 24, 2002 2:25 pm

Post by MattSharp »

Any further advice?
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Have you tried:

Code: Select all

<font size="2" color="black">Test: <?php echo $_POST['machine']; ?></font><br />
(note that the forum decided to remove the closing php tag - ? >)
and what do you get if you put

Code: Select all

echo '<pre>';
print_r($_POST);
echo '</pre>';
at the top of the form processing page?

Mac
MattSharp
Forum Commoner
Posts: 62
Joined: Wed Apr 24, 2002 2:25 pm

Post by MattSharp »

Yes I tried that, it produced the same result. Your second test produced this result.

Array
(
[key] => VALUEkey=VAULE
)

Where key is the name and VALUE is the value.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

if you switch the form's method from "POST" to "GET" what does the url look like after you pressed "submit"?
MattSharp
Forum Commoner
Posts: 62
Joined: Wed Apr 24, 2002 2:25 pm

Post by MattSharp »

volka wrote:if you switch the form's method from "POST" to "GET" what does the url look like after you pressed "submit"?
That works correctly.

EDIT: Both the URL and the variables.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

you might want to try
  • php.ini: always_populate_raw_post_data = On
  • or .htaccess: php_flag always_populate_raw_post_data On
to see what $HTTP_RAW_POST_DATA contains.

Code: Select all

<html><body>
<?php echo htmlentities($HTTP_RAW_POST_DATA); ?>
<form method="POST">
	<input type="hidden" name="machine" value="one" />
	<input type="hidden" name="machine" value="two" />
	<input type="submit" value="Select" />
</form> 
</body></html>
is mbstring or another conversion active?
MattSharp
Forum Commoner
Posts: 62
Joined: Wed Apr 24, 2002 2:25 pm

Post by MattSharp »

volka wrote:you might want to try
  • php.ini: always_populate_raw_post_data = On
  • or .htaccess: php_flag always_populate_raw_post_data On
to see what $HTTP_RAW_POST_DATA contains.

Code: Select all

<html><body>
<?php echo htmlentities($HTTP_RAW_POST_DATA); ?>
<form method="POST">
	<input type="hidden" name="machine" value="one" />
	<input type="hidden" name="machine" value="two" />
	<input type="submit" value="Select" />
</form> 
</body></html>
is mbstring or another conversion active?
That code doesn't output anything.....
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

it only does if always_populate_raw_post_data is enabled.
check with

Code: Select all

<? phpinfo(); ?>
the local value for always_populate_raw_post_data must be 1
e.g.

Code: Select all

Configuration
PHP Core
Directive                         Local Value    Master Value
allow_call_time_pass_reference      Off              Off
allow_url_fopen                      1                1
always_populate_raw_post_data        1                0
arg_separator.input                  &amp;                &amp;
arg_separator.output                 &amp;                &amp;
MattSharp
Forum Commoner
Posts: 62
Joined: Wed Apr 24, 2002 2:25 pm

Post by MattSharp »

volka wrote:it only does if always_populate_raw_post_data is enabled.
check with

Code: Select all

<? phpinfo(); ?>
the local value for always_populate_raw_post_data must be 1
e.g.

Code: Select all

Configuration
PHP Core
Directive                         Local Value    Master Value
allow_call_time_pass_reference      Off              Off
allow_url_fopen                      1                1
always_populate_raw_post_data        1                0
arg_separator.input                  &amp;                &amp;
arg_separator.output                 &amp;                &amp;
Upoon testing the output of what you told me to I got:

VALUEmachine=VALUE;

Now, I guess I just have to put an extra data field in that does nothing, although I don't know why. Could this problem and the other problem I am having with uploading files be contributed to a bad install of PHP?
Post Reply