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
LVL 3
stealthwifi12Asked:
Who is Participating?
 
amitnepalCommented:
you need to set the buttons name as submit, next and previous. There are two things, name and value for a button type. Value is what is displayed in the button and name is what is used to reference it.

and check with the code :

 if(isset($_REQUEST['submit']))
{
do this
}
else  if(isset($_REQUEST['next']))
{
do this
}
else  if(isset($_REQUEST['previous']))
{
do this
}


hope it helps..
0
 
scrathcyboyCommented:
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!
0
 
amitnepalCommented:
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
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
stealthwifi12Author Commented:
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.
0
 
stealthwifi12Author Commented:
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

0
 
stealthwifi12Author Commented:
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.