Hidden Input, Image Buttons, and Self-submitting 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
makamo66
Forum Newbie
Posts: 24
Joined: Wed May 12, 2010 6:13 pm

Hidden Input, Image Buttons, and Self-submitting Forms

Post by makamo66 »

I have a very simple survey at http://projectpotluck.com/survey/survey.php The survey is three pages long and on each page there is hidden variable which calculates how many bonus points the user has accumulated based on the number of good answers and passes the total to the next page. Instead of using a submit button, I am using an image button but I believe they are equivalent. For example for one of the survey pages I have the following code inside of the form tags:

<input type="hidden" name="bonus" value="<?php
$bonus == $_POST['bonus'];
if ($_SESSION['cooking'] == 'hobby')
$bonus += 1;
echo $bonus; ?>">

It collects the bonus posted from the previous page and adds one more bonus point if $_SESSION['cooking'] == 'hobby' After it adds or doesn't add the bonus point, the hidden value gets submitted to the next page. For some reason this code used to work before I made some improvements to the code and after "improving" the code it stopped working. Before I improved the code I wasn't using SESSION variables or image buttons. Oh and another key change was that the form became self-submitting (that is, <form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">) and after being posted to itself, it gets sent to the next page via a header, that is it gets sent with header("Location: $url");. Any idea how this can be fixed?
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Hidden Input, Image Buttons, and Self-submitting Forms

Post by califdon »

The expression $bonus == $_POST['bonus'] returns True if the 2 variables are equal, and False if they are not. Such an expression has no place in that part of your code. Here's how to do that:

Code: Select all

<?php
$bonus = $_POST['bonus'];
if ($_SESSION['cooking'] == 'hobby')  $bonus += 1;
echo "<input type='hidden' name='bonus value='$bonus'>";
?>
But since you're using session variables anyway, wouldn't it be a lot simpler to just pass the bonus points in a session variable? That's what they're for.
makamo66
Forum Newbie
Posts: 24
Joined: Wed May 12, 2010 6:13 pm

Re: Hidden Input, Image Buttons, and Self-submitting Forms

Post by makamo66 »

Okay thanks for your help. I used SESSION variables instead per your suggestion. I wasn't using them before because I didn't realize they could be changed from page to page. I thought if I set them on one page that they kept the same value but I was able to increment the SESSION variables and submit changed ones.
Post Reply