Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple POST problem I can not figure out

Status
Not open for further replies.

letmeoutofhere

Programmer
Jan 23, 2002
10
0
0
US
Hello, I am trying to use the POST process to pass variables from one form to another. Not fancy, however it is not working. Any ideas?

*****************
<html>
<head>
<title>Polling</title>
</head>

<body>
<h1>Poll </h1>

<p> Vole...</p>

<?$Age = 1?>;
<form method=post action=&quot;showpoll.php&quot;>
<input type=&quot;hidden&quot; name=&quot;test&quot; value=&quot;<?$Age?>&quot;>
<input type=submit value=&quot;Show Results&quot;></input>
</form>
</body>

*********************
showpoll.php contains:
<?
error_reporting(E_ALL);

$test = $HTTP_POST_VARS[&quot;test&quot;];

echo &quot;test = &quot;. $test;
?>


</html>

************************
The output has $test being empty.

Any ideas?

Thanks and sorry for the elementary question.

Jim
 

<?$Age = 1?>;
the ; should be after 1
<?$Age = 1; ?>

<input type=&quot;hidden&quot; name=&quot;test&quot; value=&quot;<? print $Age; ?>&quot;>

that should work
 
Thanks for the quick response, however I got the same results .. nothing passed to &quot;test&quot; variable.

Any more ideas?

Jim




 
have you tried using:

print_r($_POST); --BB
 
HorsefaceBA,

Try doing riffy's suggestion again but make sure you save the first page as 'yourname.php'. I changed your input type from hidden to text to see what value was in it, and that showed me that the first page with the form on it has to be .php. Is it already? You haven't mentioned in your post.

Binky

Binky ::)
><>
 
Thanks.. I tried using print_r in this format for the second page:

<?
error_reporting(E_ALL);

$test = $HTTP_POST_VARS['test'];

print_r($_POST['test']);
?>


</html>

but still get the same results. Also, I changed the first page to a .php extension. Same results.

The POST process works for text fields and radio buttons so I didn't know if my problem is in using the hidden attribute.

Any more ideas? I know I am out of them...

Thanks
Jim

 
do a

print &quot;<pre>&quot;;
print_r($_POST);
print &quot;</pre>&quot;;

so you can see all your post variables.... might help you debug.

-Rob
 
Thanks. I added the print_r($_POST) to my script and it verified that the $test field is coming in with blanks. I have added another text field, and when posting putting something in there, I get the data from the text field, just the test field (type=hidden) is blank.

Really frustrating...

Jim
 
TANSTAAFL,

Thanks for the question about viewing the source. I didn't see that message when I posted my last response.

When I view the source for the page with the form, there is no reference to the assignment of $Age. It is skipped completely and not placed into the page. This would explain why it is coming in blank, I'm just confused as to why it would be removed by the PHP interpreter.

THanks

Jim
 
So, the HTML source reads something like

<input type=&quot;hidden&quot; name=&quot;test&quot; value=&quot;&quot;>

right?

Did you see the comment earlier in this thread from riffy? The one about the missing semicolon in the line that assigns a value to $Age? He may have hit on the problem.

Does the script that produces the submission form generate any errors? It's possible to turn off all error reporting in PHP, so I'm wondering if your installation is reporting errors at all. Want the best answers? Ask the best questions: TANSTAAFL!
 
I finally solved the problem but am not sure why it works this way: I added the echo command into the value statement and it posted the value into the hidden variable.

***********
<?php
$Age = &quot;12&quot;;
?>

<form method=post action=&quot;showpoll.php&quot;>
<input type=&quot;hidden&quot; name=&quot;testa&quot; value='<?echo $Age;?>'></input>
<input type=&quot;text&quot; name=&quot;newtest&quot;></input>
<input type=submit value=&quot;Show Results&quot;></input>

********
Anyone know why it would do this?
 
Because that's how it's supposed to work (as Riffy told you in the first response in this thread :) )

echo & print are equivalent.

when you use the <? ?> tags you tell the php interpreter to start.

the statement
$Age;
in the php interpreter doesn't output anything to the page, in fact I don't think it does anything except touch the variable and move on, if that.

-Rob
 
I never have liked the programming style of switching between PHP-mode and HTML mode. I figure if I wanted to program in ColdFusion, I'd buy ColdFusion.

I think errors like the one in this thread post can be more easily avoided if you stay in PHP mode:
Code:
<?php

print '
<html>
	<head>
		<title>Polling</title>
	</head>
<body>
	<h1>Poll </h1>
	<p> Vole...</p>';

$Age = 1;

print '
	<form method=post action=&quot;showpoll.php&quot;>
		<input type=&quot;hidden&quot; name=&quot;test&quot; value=&quot;' . $Age . '&quot;>
		<input type=submit value=&quot;Show Results&quot;></input>
	</form>
</body>
';

?>
Want the best answers? Ask the best questions: TANSTAAFL!
 
sorry to post another question here, but about your last comment sleipnir, how much does printing statements like that affect performance? ive read in the manual that its suggested to switch back to html to give the parser a break in those kind of situations, but was curious to hear your thoughts since you certainly appear to know your stuff. what we see depends mainly on what we're looking for.
--unknown
 
I much prefer <?=$var ?> than <? echo $var ?> when embedding in HTML, it looks much cleaner. --BB
 
bedrock:

I really doubt that we have hit upon a &quot;large block of text&quot; in this case, but unfortunately the manual doesn't give indictation of what constitues an HTML block that is sufficiently large. It also does not state whether the performance difference is for printing a large block of HTML as a single large print statement or multiple smaller print statements, or whether it makes a difference.

I have no information for PHP, but context switching in some languages is very resource intensive. In ASP, for example, the very best way to degrade performance is to switch between ASP processing and HTML output. Admittedly, though, PHP's interpreter is several orders of magnitude more efficient than ASP's.

BB101:
That's just it. I don't embed PHP code in HTML. I write PHP programs that output HTML. So my code has one &quot;<?php&quot; tag as the first line of the file and one &quot;?>&quot; as the last line of the file. I need to output, I use print.

I think that if HorsefaceBA had been writing in my style, the reason for his error would have been a lot more apparent. Want the best answers? Ask the best questions: TANSTAAFL!
 
Don't forget it was very apparent to riffy, he posted to solution right off the bat, it just wasn't implemented.

I think it definately pays to use both methods depending on the issues... I have some pages with 400 lines or so of HTML & Javascript and only a half dozen or so php variables added dynamically... I much prefer to embed the PHP in those cases (the quote escaping alone makes this worthwhile for me)... however, if it's anything close to equal I will always consider a php program which outputs HTML to be easier to maintain, more readable and without a doubt in the world easier to debug.

-Rob
 
skiflyer:

True, riffy caught it right off the bat. But then, if HorsefaceBA had been writing in a &quot;PHP outputting HTML&quot; metaphor, then the issue would have been moot. A simple scan of the lines would have shown a missing line-terminating semicolon.

The deeper problem, as I see it, is that HorsefaceBA must have error reporting turned off. Otherwise that missing semicolon would have generated an error. Want the best answers? Ask the best questions: TANSTAAFL!
 
So true so true.

That's a switch that should be much harder to turn off.

Pages ought to be robust enough to handle errors before they happen, and if you intentionally need to ignore an error for one reason or another the @ is more than sufficient.

I grant at some point or another you may want it employed... but there's no reason for 90% of us to go near that.

-Rob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top