...

View Full Version : how to use two buttons with php



doforumda
10-22-2009, 10:16 AM
hi i have one form. there are three fields which are to be filled by the user. and then there are two buttons. ob clicking first button i want to save user's provided data to the database display the same page again. and on clicking second button the provided data will be saved and will be taken to next page.

my problem is with these buttons how can i set up these buttons to do the the action?

my code is here

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
</head>

<body>
<form name="form1" method="post" action="">
<label>Degree Name:
<input type="text" name="textfield" id="textfield">
</label>
<p>
<label>CGPA:
<input type="text" name="textfield2" id="textfield2">
</label>
</p>
<p>
<label>Institute:
<input type="text" name="textfield3" id="textfield3">
</label>
</p>
<p>
<label>
<input type="submit" name="button" id="button" value="Save And Add Another">
</label>
</p>
<p>
<label>
<input type="submit" name="button2" id="button2" value="Save And Proceed to Next Step">
</label>
</p>
</form>
<?php
$db = mysql_connect("localhost");
mysql_select_db("job", $db);
$addcv = mysql_query("insert into cv
(
degree,
cgpa,
institute
)
values
(
'".$degree."',
'".$cgpa."',
'".$institute."',
)");
echo "Your data is added.";
?>
</body>
</html>

Rowsdower!
10-22-2009, 02:30 PM
hi i have one form. there are three fields which are to be filled by the user. and then there are two buttons. ob clicking first button i want to save user's provided data to the database display the same page again. and on clicking second button the provided data will be saved and will be taken to next page.

my problem is with these buttons how can i set up these buttons to do the the action?

my code is here

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
</head>

<body>
<form name="form1" method="post" action="">
<label>Degree Name:
<input type="text" name="textfield" id="textfield">
</label>
<p>
<label>CGPA:
<input type="text" name="textfield2" id="textfield2">
</label>
</p>
<p>
<label>Institute:
<input type="text" name="textfield3" id="textfield3">
</label>
</p>
<p>
<label>
<input type="submit" name="button" id="button" value="Save And Add Another">
</label>
</p>
<p>
<label>
<input type="submit" name="button2" id="button2" value="Save And Proceed to Next Step">
</label>
</p>
</form>
<?php
$db = mysql_connect("localhost");
mysql_select_db("job", $db);
$addcv = mysql_query("insert into cv
(
degree,
cgpa,
institute
)
values
(
'".$degree."',
'".$cgpa."',
'".$institute."',
)");
echo "Your data is added.";
?>
</body>
</html>

When the submit button with the name "button" is pressed it will post this information to the target page. Likewise, when the submit button with the name "button2" is pressed that information will be passed to the target page as well. All you have to do is test to see which button name is part of the posted variables and then treat the submission accordingly.

Try something like this:
<?php

if (isset($_POST['button2'])) {
//process your data first
$db = mysql_connect("localhost");
mysql_select_db("job", $db);
$addcv = mysql_query("insert into cv
(
degree,
cgpa,
institute
)
values
(
'".$degree."',
'".$cgpa."',
'".$institute."',
)");

//then send user to the url for the next step
header( 'Location: http://www.yoursite.com/new_page.html' ) ;
}
else if (isset($_POST['button'])) {
//process your data and do nothing else
$db = mysql_connect("localhost");
mysql_select_db("job", $db);
$addcv = mysql_query("insert into cv
(
degree,
cgpa,
institute
)
values
(
'".$degree."',
'".$cgpa."',
'".$institute."',
)");
echo "Your data is added.";
}

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
</head>

<body>
<form name="form1" method="post" action="">
<label>Degree Name:
<input type="text" name="textfield" id="textfield">
</label>
<p>
<label>CGPA:
<input type="text" name="textfield2" id="textfield2">
</label>
</p>
<p>
<label>Institute:
<input type="text" name="textfield3" id="textfield3">
</label>
</p>
<p>
<label>
<input type="submit" name="button" id="button" value="Save And Add Another">
</label>
</p>
<p>
<label>
<input type="submit" name="button2" id="button2" value="Save And Proceed to Next Step">
</label>
</p>
</form>
</body>
</html>

doforumda
10-22-2009, 03:38 PM
yes i got your point Rowsdower but i also want this data to be posted to my target page when button2 is pressed. how can i achieve this?

Rowsdower!
10-22-2009, 06:48 PM
I think you are looking at using sessions for that. I'm not up to speed on those enough to help though.

godofreality
10-22-2009, 08:34 PM
in your section of code for button2 before the header part add this



session_start()
$_SESSION['field1'] = $_POST['field1'];
$_SESSION['field2'] = $_POST['field2'];
$_SESSION['field3'] = $_POST['field3'];

be sure to replace the field part with the form field names and what this does is starts a session and creates 3 session variables and gives them the values from the form to be recalled on anypage u chose after this and all u have to do to access these variables is include the session_start() at the top of any page u wish to use them variables on but i would recommend putting it on every page

Rowsdower!
10-22-2009, 09:12 PM
I studied up on sessions a bit and took a stab at it since I'm finding it to be a hole in my own skills.

Here is a basic sandbox file that you can take apart and get to know:
<?php
session_start();
if(isset($_SESSION)) {
if (isset($_POST['logout'])) {
//to reset page session...
unset($_SESSION['degrees']);
unset($_SESSION['cgpas']);
unset($_SESSION['institutes']);
unset($_SESSION['favorite_foods']);
unset($_SESSION['hair_colors']);
unset($_SESSION['heights']);
session_destroy();
}
else {
if(isset($_SESSION['degrees'])) {
if (isset($_POST['save_and_add']) || isset($_POST['save_and_continue'])) {
if (isset($_POST['degree_name'])) {
array_push($_SESSION['degrees'],$_POST['degree_name']);
}
}
}
else {
$_SESSION['degrees']=array();
}
if(isset($_SESSION['cgpas'])) {
if (isset($_POST['save_and_add']) || isset($_POST['save_and_continue'])) {
if (isset($_POST['cgpa'])) {
array_push($_SESSION['cgpas'],$_POST['cgpa']);
}
}
}
else {
$_SESSION['cgpas']=array();
}
if(isset($_SESSION['institutes'])) {
if (isset($_POST['save_and_add']) || isset($_POST['save_and_continue'])) {
if (isset($_POST['institute'])) {
array_push($_SESSION['institutes'],$_POST['institute']);
}
}
}
else {
$_SESSION['institutes']=array();
}
if(isset($_SESSION['favorite_foods'])) {
if (isset($_POST['save_and_add']) || isset($_POST['save_and_continue'])) {
if (isset($_POST['favorite_food'])) {
array_push($_SESSION['favorite_foods'],$_POST['favorite_food']);
}
}
}
else {
$_SESSION['favorite_foods']=array();
}
if(isset($_SESSION['hair_colors'])) {
if (isset($_POST['save_and_add']) || isset($_POST['save_and_continue'])) {
if (isset($_POST['hair_color'])) {
array_push($_SESSION['hair_colors'],$_POST['hair_color']);
}
}
}
else {
$_SESSION['hair_colors']=array();
}
if(isset($_SESSION['heights'])) {
if (isset($_POST['save_and_add']) || isset($_POST['save_and_continue'])) {
if (isset($_POST['height'])) {
array_push($_SESSION['heights'],$_POST['height']);
}
}
}
else {
$_SESSION['heights']=array();
}
}
}
else {
$_SESSION=array();
}



////////// Open Debugging Stuff

/*
if(isset($_SESSION['degrees'])) {
foreach ($_SESSION['degrees'] as $key=>$value) {
echo 'The value of $_SESSION[\'degrees\'][\''.$key.'\'] is "'.$value.'" <br />';
}
}
if(isset($_SESSION['cgpas'])) {
foreach ($_SESSION['cgpas'] as $key=>$value) {
echo 'The value of $_SESSION[\'cgpas\'][\''.$key.'\'] is "'.$value.'" <br />';
}
}
if(isset($_SESSION['institutes'])) {
foreach ($_SESSION['institutes'] as $key=>$value) {
echo 'The value of $_SESSION[\'institutes\'][\''.$key.'\'] is "'.$value.'" <br />';
}
}
*/

////////// Close Debugging Stuff





foreach($_POST as $k=>$v) {
$$k=$v;
// print $k."<br />".$v."<br />";
}



$form1 = "<form id=\"form1\" method=\"post\" action=\"\">\n <div>\n <input type=\"hidden\" name=\"step\" value=\"1\" />\n <p>\n <label>Degree Name:</label>\n <input type=\"text\" name=\"degree_name\" id=\"textfield\" />\n </p>\n <p>\n <label>CGPA:</label>\n <input type=\"text\" name=\"cgpa\" id=\"textfield2\" />\n </p>\n <p>\n <label>Institute:</label>\n <input type=\"text\" name=\"institute\" id=\"textfield3\" />\n </p>\n <p class=\"buttons\">\n <input type=\"submit\" name=\"save_and_add\" id=\"button\" value=\"Save And Add Another\" />\n <input type=\"submit\" name=\"save_and_continue\" id=\"button2\" value=\"Save And Proceed to Next Step\" />\n <input type=\"submit\" id=\"logout\" name=\"logout\" value=\"Log Out\" />\n </p>\n </div>\n</form>";
$form2 = "<form id=\"form2\" method=\"post\" action=\"\">\n <div>\n <input type=\"hidden\" name=\"step\" value=\"2\" />\n <label>Favorite Food:</label>\n <input type=\"text\" name=\"favorite_food\" id=\"textfield\" />\n <label>Hair Color:</label>\n <input type=\"text\" name=\"hair_color\" id=\"textfield2\" />\n <label>Height:</label>\n <input type=\"text\" name=\"height\" id=\"textfield3\" />\n <p class=\"buttons\">\n <input type=\"submit\" name=\"save_and_add\" id=\"button\" value=\"Save And Add Another\" />\n <input type=\"submit\" name=\"save_and_continue\" id=\"button2\" value=\"Save And Proceed to Next Step\" />\n <input type=\"submit\" id=\"logout\" name=\"logout\" value=\"Log Out\" />\n </p>\n </div>\n</form>";
$form3 = "<p>This would be form 3...</p>";

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title>Form - <?php
if (!isset($_POST['step'])) {
print "Step 1";
}
else {
if (isset($_POST['logout'])) {
print "Logged Out";
}
else {
$step_number=$_POST['step']+1;
print "Step $step_number";
}
}
?></title>
<style type="text/css">
* {margin:0;padding:0;}
body {padding:10px;}
form label {display:block;float:left;padding-right:10px;text-align:right;width:100px;clear:left;}
form input {width:200px;float:left;}
form #button, form #button2, form #logout {width:auto;}
.buttons {clear:left;}
form p {margin:0 0 10px 0;padding:0;overflow:auto;}
</style>
</head>
<body>
<?php
print "<div style=\"float:right;margin-right:20px;border:1px solid black;width:200px;padding:10px;margin-bottom:10px;white-space:pre;font-family:'trebuchet MS';font-size:12px;line-height:12px;\">\n <h3 style=\"text-decoration:underline;font-weight:bold;font-size:14px;font-family:\'trebuchet ms\';margin:0;padding:0;margin-bottom:5px;\">Current Session Data:</h3>\n";
print_r($_SESSION);
print "</div>\n";

if (isset($_POST['save_and_continue'])) {
//process your data first
/*
$db = mysql_connect("localhost");
mysql_select_db("job", $db);
$addcv = mysql_query("insert into cv
(
degree,
cgpa,
institute
)
values
(
'".$degree."',
'".$cgpa."',
'".$institute."',
)");
*/
}
else if (isset($_POST['save_and_add'])) {
//process your data and load current step's form again
/*
$db = mysql_connect("localhost");
mysql_select_db("job", $db);
$addcv = mysql_query("insert into cv
(
degree,
cgpa,
institute
)
values
(
'".$degree."',
'".$cgpa."',
'".$institute."',
)");
*/
echo "Your data has been added.";
}
?>
<?php
if (!isset($_POST['step'])) {
print $form1;
}
if (isset($_POST['step'])) {
$step=$_POST['step'];
if ($step==1) {
if (isset($_POST['save_and_add'])) {
print $form1;
}
else if (isset($_POST['logout'])) {
print $form1;
}
else {
print $form2;
}
}
else if ($step==2) {
if (isset($_POST['save_and_add'])) {
print $form2;
}
else {
print $form3;
}
}
}
?>

</body>
</html>

I have no idea if this code is efficient or secure in any way (it probably isn't efficient and it definitely needs data sanitation built in to prevent hacking attempts), but it does save the variables across the form steps just fine. The DB portions have been commented out since I didn't bother to hook up a DB to this. When you take this live you would want to adjust WHEN the information is saved or else you will potentially get a lot of redundant entries. I haven't tested or debugged for that part of the script AT ALL, only the session variables and form stages have been tested.

Also, when you go live you would want to insert error handling both via javascript and also with PHP to make sure that only valid data sets are entered into the session variables (for example, you don't want to insert blank values if the user should instead be prompted to fill out the form in its entirety).

Let me know if you have any questions about this test script...

doforumda
10-23-2009, 04:56 AM
in your section of code for button2 before the header part add this



session_start()
$_SESSION['field1'] = $_POST['field1'];
$_SESSION['field2'] = $_POST['field2'];
$_SESSION['field3'] = $_POST['field3'];

be sure to replace the field part with the form field names and what this does is starts a session and creates 3 session variables and gives them the values from the form to be recalled on anypage u chose after this and all u have to do to access these variables is include the session_start() at the top of any page u wish to use them variables on but i would recommend putting it on every page

i apply sessions but says an error. what i did is here
this is my first page

<?php
if(isset($_POST['return']))
{
$degree = $_POST['degree'];
$cgpa = $_POST['cgpa'];
$institute = $_POST['institute'];

echo $degree.'<br>';
echo $cgpa.'<br>';
echo $institute.'<br>';
}
else if(isset($_POST['next']))
{
session_start();
$_SESSION['degree'] = $_POST['degree'];
$_SESSION['cgpa'] = $_POST['cgpa'];
$_SESSION['institute'] = $_POST['institute'];

header("Location: next.php");
}
?>
]and on second page


<?php

$degree = $_POST['degree'];
$cgpa = $_POST['cgpa'];
$institute = $_POST['institute'];

echo $degree.'<br>';
echo $cgpa.'<br>';
echo $institute.'<br>';
?>

but it displays this error


Notice: Undefined index: degree in D:\wamp\www\examples\next.php on line 12

Notice: Undefined index: cgpa in D:\wamp\www\examples\next.php on line 13

Notice: Undefined index: institute in D:\wamp\www\examples\next.php on line 14


what could be the reason? why are they not getting pass to another page?
help

godofreality
10-23-2009, 05:12 AM
bcuz ur no longer posting data from one page to the next and u must declare that the session is being used on this page so u must add that b4 u try and call up any session variables



<?php
session_start()
$degree = $_SESSION['degree'];
$cgpa = $_SESSION['cgpa'];
$institute = $_SESSION['institute'];

echo $degree.'<br>';
echo $cgpa.'<br>';
echo $institute.'<br>';


try using that and see if that will work out for u

Rowsdower!
10-23-2009, 08:08 PM
Note that if you do this on each page
$_SESSION['field1'] = $_POST['field1'];
$_SESSION['field2'] = $_POST['field2'];
$_SESSION['field3'] = $_POST['field3'];

You will continually overwrite your session data with only the most recent value. Session variables work like arrays (in fact they are arrays) and you should be pushing that value onto the correct array rather than assigning it.

The code I provided works completely for session data storage so I'm not sure why you haven't worked with that example code. You might at least give it a try. :D

Anyway, even if you are going this route instead it is very important that you remember to sanitize your data and provide error checking for your forms.

No matter what method you use for your data collection if you don't sanitize your data before passing it along to the session (or worse, your database) you leave yourself open to bad data (if you're lucky) and/or many successful hacking attempts (if you aren't so lucky). For one easy example just google SQL injection...

As I said before, solutions for this should be implemented in BOTH javascript and your PHP code. Javascript is there to catch errors before they make it to the server (saving bandwidth and server load - especially for common and non-hostile data errors) and PHP is there as insurance to catch those same errors if and when they actually do reach the server. The PHP end is of paramount importance when thwarting hacking attempts since this is where you can stop a hacker from inputting a string of code to damage or infiltrate your system rather than true data.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum