Using 2 submit buttons? How?

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
TheOracle
Forum Commoner
Posts: 64
Joined: Mon Nov 22, 2004 4:56 am
Location: Bedford, UK

Using 2 submit buttons? How?

Post by TheOracle »

Hi All,

I have a complex/lengthy finance appliction form, and I have split it into 4 pages to make it more user-friendly.

However, I also want to have 2submit buttons.

1. Will save the data and go to the next paart of the app form
2. Will save the data and exit.

How do I achieve this?
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

Assign names to each of the submit buttons (saveexit, savecontinue?) then use php to determine which one was pressed.

For example, assume there is a form with two submit buttons. One is named saveexit, the other savecontinue. Here's what you'd do:

Code: Select all

if ($_POST['saveexit']) {
  // code to execute if the save and exit button was pressed
}
elseif ($_POST['savecontinue']) {
  // code to execute if the save and continue button was pressed
}
Let me know if you need me to elaborate.
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

how to do it

Post by neophyte »

two different submit buttons two different names. When you write the script you'd simply use an if statment on the page that processes the data.

Code: Select all

<?php
if ($_POST['submit'] == "name1")
{
//save the data and go to the next paart of the app form 
}
if ($_POST['submit'] == "name2")
{
// save the data and exit. 
}

?>
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post by neophyte »

Nigma your too fast!
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

lol neophyte. But hey, in all seriousness have you tested the code you posted in your reply? If so what did the forms html look like? I was testing it on my testing server and things didn't seem to work out.
TheOracle
Forum Commoner
Posts: 64
Joined: Mon Nov 22, 2004 4:56 am
Location: Bedford, UK

Post by TheOracle »

Thanks guys I'll try this
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post by phpScott »

one submit button, two different values for them then do the if statement in php code.

Code: Select all

<?php
  if(isset($_POST['button']) && ($_POST['button']=="save"))
	  echo "button pressed was save";
	elseif(isset($_POST['button']) && ($_POST['button'] == "exit"))
		 echo "button pressed was exit";
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<title>Untitled</title>
</head>
<body>
<form action="buttonpressed.php" method="post">
<input type="submit" name="button" value="save" />
<input type="submit" name="button" value="exit" />
</form>
</body>
</html>
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post by neophyte »

Myself:
]two different submit buttons two different names. When you write the script you'd simply use an if statment on the page that processes the data.
What I said was wrong. :oops: But what I meant to say was two different "values" with the same "name".

Similar to phpscott:

Code: Select all

<?php

<html>
<head>
</head>
<body>
<?php
if ($_POST['submit'] == "submit1")
{
echo "less money";
//save the data and go to the next paart of the app form 
}
if ($_POST['submit'] == "submit2")
{
// save the data and exit. 
echo "more money";
}
?>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
<input type="submit" name="submit" value="submit1">
<input type="submit" name="submit" value="submit2">
</form>
</body>
</html>
?>
TheOracle
Forum Commoner
Posts: 64
Joined: Mon Nov 22, 2004 4:56 am
Location: Bedford, UK

Post by TheOracle »

OK, I have this:

Code: Select all

if(!$error_msg && $_POST['submit'] == "Next >>")
{
	$redirect == "loan2.php";
	mysql_select_db($database_pdb_conn, $link);
	$result=mysql_query("insert into secure_loan (forename_1, forename_2, surname_1, surname_2, title_1, title_2, maiden_1, maiden_2, dob_1, 
						dob_2, address, howlongYY, howlongMM, housetype, marital, hometel, mobtel, prev_address1, prev_address2)
						values 
						( '".$_POST['forename1']."' , '".$_POST['forename2']."' , '".$_POST['surname1']."' , '".$_POST['surname2']."' ,
						'".$_POST['title1']."' ,'".$_POST['title2']."' ,'".$_POST['maiden1']."' ,'".$_POST['maiden2']."' ,'".$_POST['dob1']."',
						'".$_POST['dob2']."' ,'".$_POST['address']."' ,'".$_POST['howlongYY']."' ,'".$_POST['howlongMM']."' ,
						'".$_POST['housetype']."' ,'".$_POST['marital']."' ,'".$_POST['hometel']."' ,'".$_POST['mobtel']."' ,
						'".$_POST['prevaddress1']."' ,'".$_POST['prevaddress2']."')") or die(mysql_error());
	header("Location: ".$redirect.SID);
}
if(!$error_msg && $_POST['submit'] == "Save & Exit")
{
	$redirect =="thankyou.php";
	mysql_select_db($database_pdb_conn, $link);
	$result=mysql_query("insert into secure_loan (forename_1, forename_2, surname_1, surname_2, title_1, title_2, maiden_1, maiden_2, dob_1, 
						dob_2, address, howlongYY, howlongMM, housetype, marital, hometel, mobtel, prev_address1, prev_address2)
						values 
						( '".$_POST['forename1']."' , '".$_POST['forename2']."' , '".$_POST['surname1']."' , '".$_POST['surname2']."' ,
						'".$_POST['title1']."' ,'".$_POST['title2']."' ,'".$_POST['maiden1']."' ,'".$_POST['maiden2']."' ,'".$_POST['dob1']."',
						'".$_POST['dob2']."' ,'".$_POST['address']."' ,'".$_POST['howlongYY']."' ,'".$_POST['howlongMM']."' ,
						'".$_POST['housetype']."' ,'".$_POST['marital']."' ,'".$_POST['hometel']."' ,'".$_POST['mobtel']."' ,
						'".$_POST['prevaddress1']."' ,'".$_POST['prevaddress2']."')") or die(mysql_error());
	header("Location: ".$redirect.SID);
}
It seems to redirect me to my root directory whichever button I press?? Probably something simple. The database is being updated so I know the SQL works. The fact that it is redirecting is also pretty promising. The 2 files which it is directing to are also both in the root directory.

Any ideas?
TheOracle
Forum Commoner
Posts: 64
Joined: Mon Nov 22, 2004 4:56 am
Location: Bedford, UK

Post by TheOracle »

its ok I figured it out. I had

Code: Select all

$redirect == "page.php";
instead of

Code: Select all

$redirect = "page.php";
Thanks anyway
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post by phpScott »

what is the value of

Code: Select all

<?php
 header("Location: ".$redirect.SID);
?>
?

does the location actually say thanky.php?somename=somevalue;?

what is SID?
TheOracle
Forum Commoner
Posts: 64
Joined: Mon Nov 22, 2004 4:56 am
Location: Bedford, UK

Post by TheOracle »

Thnkas phpScott, but I got it working (must have beaten you to it). SID is session id, I think??? I have been obtaining some help from a book. MY understanding is that it carries over the session id varibale which from where I'm using session_start() in my included file?
Post Reply