Page 1 of 1

Using 2 submit buttons? How?

Posted: Wed Nov 24, 2004 4:21 pm
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?

Posted: Wed Nov 24, 2004 4:25 pm
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.

how to do it

Posted: Wed Nov 24, 2004 4:27 pm
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. 
}

?>

Posted: Wed Nov 24, 2004 4:28 pm
by neophyte
Nigma your too fast!

Posted: Wed Nov 24, 2004 9:05 pm
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.

Posted: Wed Nov 24, 2004 11:42 pm
by TheOracle
Thanks guys I'll try this

Posted: Thu Nov 25, 2004 3:30 am
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>

Posted: Thu Nov 25, 2004 5:21 am
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>
?>

Posted: Thu Nov 25, 2004 8:32 am
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?

Posted: Thu Nov 25, 2004 8:42 am
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

Posted: Thu Nov 25, 2004 8:44 am
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?

Posted: Thu Nov 25, 2004 8:47 am
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?