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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Keep value when form is resubmitted several times

Status
Not open for further replies.

alexisb

Programmer
Apr 5, 2001
100
0
0
US
My php page has 3 dynamic drop-downs reading data from a MySQL db with select statements like this <select name="VehicleYear1" onchange="this.form.submit()">;. The drop-downs are all working fine, however, the first time the drop-down submits to the same page, I lose the $numcars value that I need to keep resubmitting to the form. All 3 drop-downs are contained within the same form.
<form action="selectpagetest2.php" method="post" name="SelectVehicle">
<input type="hidden" name="NumberofVehicles" value="<?=$numcars?>">
<?
$numcars=$_POST['NumOfCars2Quote'];
?>

I've tried many different ways (if, case, etc) to keep that initial value but I keep losing it when the drop-down submits to the form. It works if I just plug $numcars with a value like "ten" ($numcars = "ten";) but not if I try to do the assignment based on the value (NumOfCars2Quote) passed from the previous page. If I could somehow set that value the first time the page opens and then set it as a constant so it can't change every time the form is resubmitted, that would be great.

Any help would be appreciated.
Thanks,
Alexis
 
Whenever I need to keep data intact between runs of PHP scripts, I use session variables.

With session variables, when your script regenerates the form, it can pre-set the values of the dropdowns using "SELECTED" attributes in the appropriate places.


Want the best answers? Ask the best questions! TANSTAAFL!
 
Thanks, sleipnir214, but the problem isn't with the drop-downs. I used SELECTED along with the MySQL data to build that logic. The problem is that when a user selects from the drop-down, the form is regenerated and I lose a value sent from a previous page. For example, if the value "two" is sent from the previous page, I see it the first time the second page loads. But as soon as I select a value from the first drop-down, the page is resubmitted to itself and that value goes away. I want to set that value so it is not lost each time the second page is resubmitted. I hope this makes sense.
Alexis
 
Are you setting the NumOfCars2Quote form element each time, too? If not, then that could be your problem.

To antagonize me, a friend of mine once said, "The computer is doing EXACTLY what you've programmed it to do." And she was right!

Lee
 
Thanks, everyone. I asked a friend to look at this for me as I was getting very frustrated. It turns out that the statement "$numcars=$_POST['NumOfCars2Quote'];" should have been at the top of the page, and not inside the form statement, which was being submitted each time. I looked at this for two days and couldn't figure out what I did wrong and it was this simple thing that I would NEVER have realized. I don't know why I put that line so far down - normally I do not do this.
Thanks to you all for your help.
Regards,
Alexis
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top