Jump to content


Photo

Passing variables


  • Please log in to reply
4 replies to this topic

#1 WildGoose

WildGoose
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 02 May 2006 - 06:48 AM

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:

<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>

Then, on the second page (q2.php):
 <?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>

And finally, the result page (result.php):
 <?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>

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?

#2 OOP

OOP
  • Members
  • PipPipPip
  • Advanced Member
  • 149 posts
  • LocationSaudi Arabia

Posted 02 May 2006 - 07:33 AM

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

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

regards
<?php echo'The man who makes no mistakes does not usually make anything'; ?>

#3 WildGoose

WildGoose
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 03 May 2006 - 12:03 AM

Thanks, OOP. Unfortunately, that had no effect in the result.

#4 WildGoose

WildGoose
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 03 May 2006 - 01:08 AM

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?

#5 WildGoose

WildGoose
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 03 May 2006 - 06:03 AM

I figured it out!

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

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

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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users