fohanlon Posted March 14, 2006 Share Posted March 14, 2006 Hi All,I have a form with two submit buttons on it:<form method="POST" action="' . $page . '">....<input class="formbuttons" type="submit" value="Add Another Row" name="submit"><input type="submit" value="Next" class="formbuttons" name="submit2"></form>I want it so that when a user clicks the Add Another Row the page refreshes and a new row appears. I have this working through some help I received previously.i.e.if (isset($_POST['submit']) && $_POST['submit'] == "Add More People") {$num_persons++;$page = $_SERVER['PHP_SELF'];}However, I also want to allow the user the other option that if the other submit is clicked the $page variable is set to health3.php and the form submits and goes to this page.I tried:if (isset($_POST['submit2']) && $_POST['submit2'] == "Next") $page ="health3.php";but this goes to health3.php when either of the submit buttons are clicked.Any help would be greatly appreaciated.Kind Regards,Fergal. Quote Link to comment https://forums.phpfreaks.com/topic/4975-control-logic-for-2-submit-buttons-on-the-one-form/ Share on other sites More sharing options...
obsidian Posted March 14, 2006 Share Posted March 14, 2006 you've got to keep in mind that input, hidden, submit, textarea, select fields (with the possible addition of a couple others) are ALWAYS set if your form has been submitted. so, if you click on one submit button, they will both always return true from the isset() function. your best bet to do something like this is with javascript. there are a couple options:1) use javascript to actually create your next input field instead of reloading the page2) use javascript to change the action of the form before submission to match the button that was pressedbeyond this, you'll have to come up with something a bit more intricate than simply checking to see which button isset().hope this helps Quote Link to comment https://forums.phpfreaks.com/topic/4975-control-logic-for-2-submit-buttons-on-the-one-form/#findComment-17541 Share on other sites More sharing options...
kenrbnsn Posted March 14, 2006 Share Posted March 14, 2006 You don't need to resort to javascript to solve this. I use multiple submit buttons on many of my forms with no problems at all. The trick is to give all the submit buttons the same name.Then you could use the "if" statements or "switch()" statements to control what is done.Ken Quote Link to comment https://forums.phpfreaks.com/topic/4975-control-logic-for-2-submit-buttons-on-the-one-form/#findComment-17567 Share on other sites More sharing options...
fohanlon Posted March 14, 2006 Author Share Posted March 14, 2006 Hi KenDo you mean something like this:Form:<form method="POST" action="' . $page . '"><input class="formbuttons" type="submit" value="Add Another Row" name="submit"><input type="button" value="Next" class="formbuttons" name="submit"></form>When form is submitted:if (isset($_POST['submit']) && $_POST['submit'] == "Add More People") {$page = $_SERVER['PHP_SELF']; }if (isset($_POST['submit']) && $_POST['submit'] == "Add More People") {$page = 'health3.php';}Many thanks,Fergal. Quote Link to comment https://forums.phpfreaks.com/topic/4975-control-logic-for-2-submit-buttons-on-the-one-form/#findComment-17577 Share on other sites More sharing options...
keeB Posted March 14, 2006 Share Posted March 14, 2006 [code]<input type="submit" name="submit"><input type="submit" name="addrow"><?phpif ($_POST["addrow"]) { //add row} else if ($_POST["submit"] { // form submitted}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/4975-control-logic-for-2-submit-buttons-on-the-one-form/#findComment-17602 Share on other sites More sharing options...
fohanlon Posted March 15, 2006 Author Share Posted March 15, 2006 Thanks KBeeI did the following:if (isset($_POST['addrow']) && $_POST['addrow'] == "Add Row") {$page = $_SERVER['PHP_SELF']; }else if (isset($_POST['submit']) && $_POST['submit'] == "Next") {$page = "health3.php";}<form method="POST" action="' . $page . '">....<input class="formbuttons" type="submit" value="Add Row"name="addrow"><input type="submit" value="Next" class="formbuttons" name="submit"></form>But the submit buttons are not functioning as they should. When I click either button I go to health3.phpThanks,Fergal. Quote Link to comment https://forums.phpfreaks.com/topic/4975-control-logic-for-2-submit-buttons-on-the-one-form/#findComment-17616 Share on other sites More sharing options...
hitman6003 Posted March 15, 2006 Share Posted March 15, 2006 I would use javascript for this. You can create the table and form elements as you go along, then when the submit button is pressed, the page is sent on. Then you aren't constantly going to the server for, really, no reason. Quote Link to comment https://forums.phpfreaks.com/topic/4975-control-logic-for-2-submit-buttons-on-the-one-form/#findComment-17618 Share on other sites More sharing options...
fohanlon Posted March 15, 2006 Author Share Posted March 15, 2006 Thought about this i.e. create the second submit as a type button and call a javascript function that will window.location the new page.However, I had 2 reservations:1. I wanted to try do this without Javascript and2. If I use the javascript method i.e window.location='health3.php' can I then use post on the health3 page to pull all info from submitted pageThanks,Fergal. Quote Link to comment https://forums.phpfreaks.com/topic/4975-control-logic-for-2-submit-buttons-on-the-one-form/#findComment-17619 Share on other sites More sharing options...
hitman6003 Posted March 15, 2006 Share Posted March 15, 2006 Don't redirect anywhere to add the new table rows. Use js to create them when the button is pressed.[code]<script type="text/javascript"> function addrow() { var t = document.getElementById('testtable'); var newrow = document.createElement("tr"); var newrtd = document.createElement("td"); var newltd = document.createElement("td"); newrtd.appendChild(document.createTextNode("test row 2")); newltd.appendChild(document.createTextNode("test row 2")); newrow.appendChild(newltd); newrow.appendChild(newrtd); t.appendChild(newrow); }</script><form name="testform" id="testform"><table id="testtable"> <tr> <td>sample row</td> <td>sample row</td> </tr></table><input type="button" value="add row" onclick="addrow();"></form>[/code]Not exactly what your asking for, but you get the idea. Quote Link to comment https://forums.phpfreaks.com/topic/4975-control-logic-for-2-submit-buttons-on-the-one-form/#findComment-17633 Share on other sites More sharing options...
fohanlon Posted March 15, 2006 Author Share Posted March 15, 2006 Understand where you are coming from.Soved the problem using Javascript but would still be interested to find out how I could have solved it using 2 buttons on the form through php only.Any ideas - any one? Quote Link to comment https://forums.phpfreaks.com/topic/4975-control-logic-for-2-submit-buttons-on-the-one-form/#findComment-17642 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.