Link to home
Start Free TrialLog in
Avatar of stealthwifi12
stealthwifi12Flag for United States of America

asked on

Submit form then redirect based on button pressed

I have a form with 3 submit buttons.
When the first is pressed the form is submitted then redirected.
When the 2nd is pressed it needs to submit then go to the next record form page (i have this code done)
When the 3rd is pressed it needs to submit then go to the previous record form page (i have this code done).

What I am having trouble with is determining which submit button the user pressed.
How can I set a variable or something so that when they push the save and next button it submits the form and redirects to the proper page and so forth?

The form will submit to $_SERVER['PHP_SELF'] so the code to see which button was used will go in a if(isset($_REQUEST['submit'])) section on the same page as the form resides.

note: I am unable to change the type of form or method of submitting
ASKER CERTIFIED SOLUTION
Avatar of amitnepal
amitnepal
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You solve this problem by having 3 SEPARATE forms with individual submit buttons for each form.  HTML was never designed to have more than one submit button (technically, one action) per form.  The way to change it is so simple !!  After the first submit button, close the form and start a new one.  After the 2nd submit button, close that form and start the third.  That way each form has its OWN redirect.  This is the way HTML was designed to use forms, and if you follow it, it is hardly any extra coding, and it just works!
Hi,
  Please check the above code provided it should work. It works on that way i have implemented the same. If it doesnot work for any reason, please let me know, i will post the working code.


thanks
Amit
Avatar of stealthwifi12

ASKER

amitnepal: Since this will be on one form how will I setup the buttons to submit the form and set the value to be caught in your code?

I can not use hidden field's as all of them will be set when the form is submitted


Note - I have solved this another way but am interested if yours can work.
I got it, your code worked so I'll give you the points. Below is how I used the buttons for future people:
(I dont like using a submit button with the name submit so I used submit1)

<button type="submit" name="submit1" value="submit1">Save Changes</button>
<button type="submit" name="previous" value="previous">Previous</button>
<button type="submit" name="next" value="next">Next</button>


The method I had used was via java script (attached)
<script language="JavaScript">
<!--
function submit_this(idi)
{
document.getElementById("prev2").value=idi
document.forms['zoeken'].submit();
}
-->
</script>


if ($_REQUEST['prev2']=="next")

if ($_REQUEST['prev2']=="prev")

<form name="zoeken" method="post" action="<?php print $_SERVER['PHP_SELF'];?>">

<button type="button" onclick="submit_this('prev');"><-- Previous</button>

<button type="button" onclick="submit_this('next');">Next --></button>

<input type="hidden" id="prev2" name="prev2" value="" />

</form>

Open in new window

I recommend a more detailed solution in the future, since I am not familiar with HTML adding the 3 example buttons would have been nice.