Jump to content

Passing variables


WildGoose

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?
Link to comment
https://forums.phpfreaks.com/topic/8870-passing-variables/
Share on other sites

Archived

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

×
×
  • Create New...

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.