doforumda Posted October 22, 2009 Share Posted October 22, 2009 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> Link to comment https://forums.phpfreaks.com/topic/178577-how-to-use-two-buttons-with-php/ Share on other sites More sharing options...
Gayner Posted October 22, 2009 Share Posted October 22, 2009 easy? name ur form name="test #1" then use $_POST['test #1'] echo else if $_POST['test #2 name form'] echo bla bla.. lol Link to comment https://forums.phpfreaks.com/topic/178577-how-to-use-two-buttons-with-php/#findComment-941763 Share on other sites More sharing options...
doforumda Posted October 22, 2009 Author Share Posted October 22, 2009 you mean i have to set up two forms Link to comment https://forums.phpfreaks.com/topic/178577-how-to-use-two-buttons-with-php/#findComment-941769 Share on other sites More sharing options...
Adam Posted October 22, 2009 Share Posted October 22, 2009 No you can just use two submit buttons, but give them a different name so you can identify which was pressed. For example: <form ...> <input type="submit" name="return" value="Save and return" /> <input type="submit" name="next" value="Save and move on" /> </form> if (isset($_POST['return'])) { // ... } elseif (isset($_POST['next'])) { // ... } Only the button that was clicked will be posted. Link to comment https://forums.phpfreaks.com/topic/178577-how-to-use-two-buttons-with-php/#findComment-941772 Share on other sites More sharing options...
Gayner Posted October 22, 2009 Share Posted October 22, 2009 He can set up 2 forms if he wants. Link to comment https://forums.phpfreaks.com/topic/178577-how-to-use-two-buttons-with-php/#findComment-941775 Share on other sites More sharing options...
Adam Posted October 22, 2009 Share Posted October 22, 2009 Well yeah if he really wanted to, but that would require double the amount of fields. As you can see from his code though that's not what he was after. Link to comment https://forums.phpfreaks.com/topic/178577-how-to-use-two-buttons-with-php/#findComment-941787 Share on other sites More sharing options...
doforumda Posted October 22, 2009 Author Share Posted October 22, 2009 yes i want to use just one form Link to comment https://forums.phpfreaks.com/topic/178577-how-to-use-two-buttons-with-php/#findComment-941795 Share on other sites More sharing options...
doforumda Posted October 22, 2009 Author Share Posted October 22, 2009 i am doing using the idea of your's MrAdam but it is not working. can you tell me what i am doing wrong here <?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'])) { echo '<form name="form1" method="post" action="next.php">'; } ?> Link to comment https://forums.phpfreaks.com/topic/178577-how-to-use-two-buttons-with-php/#findComment-941800 Share on other sites More sharing options...
Adam Posted October 22, 2009 Share Posted October 22, 2009 In what way does it not work? How do you have the submit buttons setup? Link to comment https://forums.phpfreaks.com/topic/178577-how-to-use-two-buttons-with-php/#findComment-941806 Share on other sites More sharing options...
doforumda Posted October 22, 2009 Author Share Posted October 22, 2009 the button with return name works but the second button does not take me to the next page Link to comment https://forums.phpfreaks.com/topic/178577-how-to-use-two-buttons-with-php/#findComment-941809 Share on other sites More sharing options...
Adam Posted October 22, 2009 Share Posted October 22, 2009 It won't do, you're just echoing a form tag? You'll need to use the header function to redirect: header("Location: next.php"); Link to comment https://forums.phpfreaks.com/topic/178577-how-to-use-two-buttons-with-php/#findComment-941812 Share on other sites More sharing options...
doforumda Posted October 22, 2009 Author Share Posted October 22, 2009 this is what i am want to do. the user will enter his qualification. he will enter more then one qualification step by step. when he enters first qualification then if he wants to enter second one then he will press the button with return name so his data will be entered into db and will face again the same form to enter second qualification(degree/certification). then if he wants to enter another then click the "Save And Add Another" button. save this data into db and then he will face the same form. once he finishes with this then he will click "Save And Proceed to Next Step" button to save the last qualification in db and will be passed to the next page but this time insert query will be on next page to enter the last qualification into db. by using header() ftn this goes to next page but data is not passing there. my next page is here <?php $degree = $_POST['degree']; $cgpa = $_POST['cgpa']; $institute = $_POST['institute']; echo $degree.'<br>'; echo $cgpa.'<br>'; echo $institute.'<br>'; ?> Link to comment https://forums.phpfreaks.com/topic/178577-how-to-use-two-buttons-with-php/#findComment-941837 Share on other sites More sharing options...
doforumda Posted October 22, 2009 Author Share Posted October 22, 2009 anyone who can solve my problem? Link to comment https://forums.phpfreaks.com/topic/178577-how-to-use-two-buttons-with-php/#findComment-941890 Share on other sites More sharing options...
cags Posted October 22, 2009 Share Posted October 22, 2009 Using header redirects which will clear the $_POST array, you will either have to self post and simply check what stage your at and output the approriate information. Alternatively use the $_SESSION array to persist the data before calling the header function. Link to comment https://forums.phpfreaks.com/topic/178577-how-to-use-two-buttons-with-php/#findComment-941932 Share on other sites More sharing options...
doforumda Posted October 22, 2009 Author Share Posted October 22, 2009 can you show me how can i use $_SESSION array in this code? i didnt use this before. Link to comment https://forums.phpfreaks.com/topic/178577-how-to-use-two-buttons-with-php/#findComment-941980 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.