Jump to content

Archived

This topic is now archived and is closed to further replies.

WildGoose

Passing variables

Recommended Posts

I want to build a series of very simple pages as an exercise, where a variable is passed via a form from one page to the next, where a calculation is performed on the variable's numeric value, and then passed via a form to a third page. I can't seem to get it to work. The first page--I'll call it q1.php--has something like this:

[code]
<html>
<body>
<form action="q2.php" method="post">
<p>
<b>1. Choose an option:</b><br>
<input type="radio" name="q1" value="Increment"> Increment <br>
<input type="radio" name="q1" value="NoIncrement"> No Increment<br>
<br>
<input type="submit" value="Go to the next page">
</form>
</body>
</html>
[/code]

Then, on the second page (q2.php):
[code]
<?php
$score = 0;
$q1 = $_POST['q1'];
if ($_POST['q1'] == 'Increment')
$score++;
?>
<html>
<body>
<form action="result.php" method="post">
<p>
<b>2. Choose an option:</b><br>
<input type="radio" name="q2" value="Increment"> Increment <br>
<input type="radio" name="q2" value="NoIncrement"> No Increment<br>
<input type="hidden" name="score" value="<? $_POST['score']; ?>">
<br>
<input type="submit" value="Go to the next page">
</form>
</body>
</html>
[/code]

And finally, the result page (result.php):
[code]
<?php
$score = $_POST['score'];
$q2 = $_POST['q2'];
if ($_POST['q2'] == 'Increment')
$score++;
?>
<html>
<body>
<?php
echo '<b>The number of increments was ' . $score . '</b><br><br>';
?>
</body>
</html>
[/code]

So, starting at q1.php, this little trio of pages asks whether the user wants to increment twice. Then on the third page, what I WANT it to do is display the number of times the user told it to increment. However, it doesn't; it says there was only one increment if the user chose that option on q2.php, and the variable $score is NULL if no increment is selected on q2. I know this is probably completely elementary, but I'm new at this.

What am I doing wrong here?

Share this post


Link to post
Share on other sites
Hi there,
in the second form try to do this....change the following :
[code]<input type="hidden" name="score" value="<? $_POST['score']; ?>">[/code]

to this:
[code]<input type="hidden" name="score" value="<? $score; ?>">[/code]

regards

Share this post


Link to post
Share on other sites
I tried changing the POSTs to GETs so that I could easily see the variable values being passed. Interestingly, the URL of the third page ends in:

result.php?q2=Increment&score=

"score" has no value. Is that a clue as to what might be happening here?

Share this post


Link to post
Share on other sites
I figured it out!

I had to change this:
[code]<input type="hidden" name="score" value="<? $score; ?>">[/code]

to this:
[code]<input type="hidden" name="score" value="<? echo $score; ?>">[/code]

I guess I assumed the echo was implied, but apparently it has to be there.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.