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

using ($_POST['Submit']) for 2 different post outcomes

Status
Not open for further replies.

kenabbott

Programmer
Mar 16, 2005
6
GB
Hi

I have a PHP page with a number of dropdowns on it and a submit button. When a value is chosen from a dropdown I need the page to refresh (a total is calculated). However when the submit button is pressed I need to go to another page which uses the values from the first page. I have tried to do this as follows:

if (isset ($_POST['Submit']) ) {
echo '<form name="form1" method="post" action="form2.php">';
}
else {
echo '<form name="form1" method="post" action="form1.php">';
}

?>

However this only partially works as the following problems occur:

1. The first time you hit the submit button form1 refreshes - if however you hit submit a second time then form2 opens as it it supposed to.

2. If you go back from form2 to form1, form2 is then always opened regardless of whether you hit submit or choose a value from the dropdowns.

Where are my going wrong??

Many thanks

Ken
 
When you say "go back", how do you do that? The browser's back button? Please explain.
 
Sorry, yes by go back I mean the browsers back button
 
This really is messy, because you are dealing with browsers and different settings.
When you go back you might get a) a cached page (which is just exactly what the server sent before), saying to post to form2 (because that's what your script printed when the page was created).
b) The browser might note that the page was generated by POST and promt you - if you say 'yes' then the page will reload with the posted info (which also has the 'submit' alsready in it.

You can deal with this in several ways:
1. Client side you could make an onChange event for the dropdown that refreshes the page and sets the rm action to the script itself.
2. Change also the text on the submit button (first page says 'caluculate total' or something like that, second says 'Submit'. Provide a link to a 'fresh' first page on the final page.
 
Thanks - I will try these out.

Re the first problem - (The first time you hit the submit button form1 refreshes, if however you hit submit a second time then form2 opens as it it supposed to.) - any ideas on this??
 
I don't follow.
The initial page creation sets the action to 'form1.php' by printing out the form tag with the action attribute set.
When the form refreshes the form tag is printed with different information.
Now, what I don't get, what is the question? Where is the problem (besides the back button)?
 
When I change value in the dropdowns form1.php reloads as required ie

<select name="lisDigital" id="lisDigital" onChange='submit()'>

When I hit the submit button the first time, form1.php reloads (which it shouldn't) - however if I hit immediately again, form2.php opens, which is what is required. The problem seems to be that when submit is hit for the first time the:

(isset ($_POST['Submit']) ) {... etc

doesn't work and form1 is reloaded. However hitting it immediately again does work!!??
 
I see now.
Hitting a submit button actually puts a variable into the POST data which is not there when the form is submitted by e.g. a onChange event.
Therefore the script does get the data when the dropdown is changed, but no button was pressed, hence no $_POST['Submit'] variable is present.

So, the solution is to send something along with the onChange event that tells the script that the page was submitted.
Put a hidden field in the form with an empty value.
Make the onChange event set a value to that hidden field.
Check for the presence of the hidden field in the $_POST array and change the output for the from action.

This should solve your problem.
 
Thanks for the info. I am fairly new to PHP so it would be possible for you to give code examples of how to achieve the above.

Many thanks

Ken
 
add a hidden field inside the form tags:
<input type="hidden" name="action" id="action" value="" />

assign an id to the form (<form method="post" action="somepage.php" id="mainform">)

then attach some javascript to your button:
onclick="submitform('action'); return true;"

then add a javascript function into the <head> tags:
Code:
<script type="text/javascript">
function submitform(val)
{
  document.getElementById("action").value=val;
  document.getElementById("mainform").submit();
}
</script>

KEY THING TO MAKE THIS WORK - ensure that you DO NOT have any button called "submit" in your form. this will stop the javascript from working. if you want the button to have submit written on it then fine. call it something else though:
<input type="submit" name="go" value="Submit" />

lastly, in your receiving script, test the value of $_POST['action'] rather than the submit button (which is now called $_POST['go']). you can, of course, change the action just by changing the value in the javascript onclick tag.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top