jeff5656 Posted December 24, 2008 Share Posted December 24, 2008 How can I have 2 submit buttons that goes to a php file and executes a different action? Here is the form: <form name="newsubmit" method="post" action="addbill.php?action=point1"> <form name="newsubmit2" method="post" action="addbill.php?action=point2"> <input name="dx1" type="text" size="20" /> <input name="dx2" type="text" size="20" /> <input name="dx3" type="text" size="20" /> <input name="dx4" type="text" size="20" /> <input type="hidden" name ="id_incr" value="<?php echo $id_incr; ?>"/> <input type="submit" value="Add and continue" onClick="return check('newsubmit', this.name)"/> <input type="submit" value="Add and return to main view" onClick="return check('newsubmit2', this.name)"/> Here I want to set up a switch to point to a differnt location depending on what button they pressed in the form addbill.php: snipped lines that update the database based on the POSTed form mysql_query($query) or die(mysql_error()); switch ($_GET['action']) { case "point1": header("Location: editdos.php?action=edit&id=".$pt_id); break; case "point2": header("Location: ../billview.php"); break; } ?> When I run this, point1 case is always executed, even if I press the second submit button. What am I doing wrong? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/138337-solved-having-to-submit-buttons-for-same-form/ Share on other sites More sharing options...
MadTechie Posted December 24, 2008 Share Posted December 24, 2008 Yeah you can't really have a form in a form like that <form name="newsubmit" method="post" action="addbill.php"> <input name="dx1" type="text" size="20" /> <input name="dx2" type="text" size="20" /> <input name="dx3" type="text" size="20" /> <input name="dx4" type="text" size="20" /> <input type="hidden" name ="id_incr" value="<?php echo $id_incr; ?>"/> <input type="submit" name="point1" value="Add and continue" onClick="return check('newsubmit', this.name)"/> <input type="submit" name="point2" value="Add and return to main view" onClick="return check('newsubmit2', this.name)"/> snipped lines that update the database based on the POSTed form mysql_query($query) or die(mysql_error()); if(isset($_POST['point1'])) { header("Location: editdos.php?action=edit&id=".$pt_id); } if(isset($_POST['point2'])) { header("Location: ../billview.php"); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/138337-solved-having-to-submit-buttons-for-same-form/#findComment-723341 Share on other sites More sharing options...
Sakesaru Posted December 24, 2008 Share Posted December 24, 2008 Of course you can have a form like that.... <?php if($_GET['button1']=="cake") { echo 'Button1 was clicked, run this code'; } else if ($_GET['button2']=="cheese") { echo 'Button2 was pressed, run that code'; } else { echo 'No button was pressed, you should press one so they don\'t feel unloved'; } ?> <form method="GET" action="/test.php"> <button name="button1" value="cake">Button1</button> <button name="button2" value="cheese">Button2</button> </form> Easy do. Whichever button you click will send it's value to the action="" page for the form, all the others will not send their value to the action="" page. Quote Link to comment https://forums.phpfreaks.com/topic/138337-solved-having-to-submit-buttons-for-same-form/#findComment-723347 Share on other sites More sharing options...
jeff5656 Posted December 24, 2008 Author Share Posted December 24, 2008 Thanks Sakesaru, I already tried the one suggested by madtechie and that works but I will keep yours in mind if I need to do this again. Not sure which one is "better". Quote Link to comment https://forums.phpfreaks.com/topic/138337-solved-having-to-submit-buttons-for-same-form/#findComment-723354 Share on other sites More sharing options...
Sakesaru Posted December 24, 2008 Share Posted December 24, 2008 I don't mean to undermine MadTechie or anything, but personally I believe it's better practice to use a non-javascript solution when one exists. Quote Link to comment https://forums.phpfreaks.com/topic/138337-solved-having-to-submit-buttons-for-same-form/#findComment-723360 Share on other sites More sharing options...
MadTechie Posted December 24, 2008 Share Posted December 24, 2008 I don't mean to undermine MadTechie or anything, but personally I believe it's better practice to use a non-javascript solution when one exists. Doesn't undermine mine as it mine doesn't use JS.... the javascript was in his existing code and probably does some validation.. The reason i didn't use the submit button value is due to the fact the values were are used to prompt the user and chaging those values would mean your need to update the php code.. when the button name are not seen by the (general) user.. thus updating the interface won't effect the code.. Quote Link to comment https://forums.phpfreaks.com/topic/138337-solved-having-to-submit-buttons-for-same-form/#findComment-723398 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.