Dragen Posted April 30, 2007 Share Posted April 30, 2007 Hi, My first question is more html than php but it links in with my next question so... Is it possible to have two submit buttons on one form? I would use different names for them, one being name="submit" and the other being name="reset".. Please note that it's not a reset button in the usual sense. I want it to submit the form as a submit button would, but as it's carrying a different name I want it to make the output different. They would both be type="submit" This leads onto my second question.. is it possible to detect what the name of the submit button is? I guess if I have two I could use something like: <?php if(isset($_POST['submit'])){ // code here }elseif(isset($_POST['reset'])){ // code here } ?> would this work? Link to comment https://forums.phpfreaks.com/topic/49279-solved-two-submit-buttons-on-one-form-and-detect-which-one-is-pressed/ Share on other sites More sharing options...
neel_basu Posted April 30, 2007 Share Posted April 30, 2007 If you are trying to use 2 Submit Buttons you can use 2 separate forms for separate Submit Buttons. e.g. <form><!-- Submit1 Button here --></form> <form><!-- Submit2 Button here --></form> And 2 form action will be Different or same.php?form=1 and same.php?form=2 Link to comment https://forums.phpfreaks.com/topic/49279-solved-two-submit-buttons-on-one-form-and-detect-which-one-is-pressed/#findComment-241463 Share on other sites More sharing options...
Dragen Posted April 30, 2007 Author Share Posted April 30, 2007 thanks. It seems to be working how I thought though.. with the isset(). At the moment I've got: <?php if(isset($_POST['submit'])){ echo "submit"; }elseif(isset($_POST['reset'])){ echo "reset"; }else{ echo "<p align=\"center\">error: unspecified action</p>"; } ?> which seems to work as when I click on the submit buttons it echoes the correct statement. I'll have to see how it works once I add all my code in though! Link to comment https://forums.phpfreaks.com/topic/49279-solved-two-submit-buttons-on-one-form-and-detect-which-one-is-pressed/#findComment-241479 Share on other sites More sharing options...
kenrbnsn Posted April 30, 2007 Share Posted April 30, 2007 The other way to do this is to give each "submit" button the same name, but different values: [cpde] <input type="submit" name="submit" value="Submit"> <input type="submit" name="submit" value="Reset"> Then in your script: <?php if (isset($_POST['submit'])) switch($_POST['submit']) { case 'Submit': echo 'The "Submit" button was pressed'; break; case 'Reset': echo 'The "Reset" button was pressed'; break; default: echo 'Problem with the submit button -- wrong value -- ' . htmlentities($_POST['submit']); break; } ?> Ken Link to comment https://forums.phpfreaks.com/topic/49279-solved-two-submit-buttons-on-one-form-and-detect-which-one-is-pressed/#findComment-241486 Share on other sites More sharing options...
Dragen Posted April 30, 2007 Author Share Posted April 30, 2007 thanks. Would this that method be any better.. in respect of cleaner code, faster loading etc? Link to comment https://forums.phpfreaks.com/topic/49279-solved-two-submit-buttons-on-one-form-and-detect-which-one-is-pressed/#findComment-241488 Share on other sites More sharing options...
kenrbnsn Posted April 30, 2007 Share Posted April 30, 2007 I think the "Switch" statement is easier to understand than many if-elseif-else statement. It's also easier to add more conditions. YMMV. Ken Link to comment https://forums.phpfreaks.com/topic/49279-solved-two-submit-buttons-on-one-form-and-detect-which-one-is-pressed/#findComment-241605 Share on other sites More sharing options...
Dragen Posted April 30, 2007 Author Share Posted April 30, 2007 yeah, I see your point. Thanks for the advice! Link to comment https://forums.phpfreaks.com/topic/49279-solved-two-submit-buttons-on-one-form-and-detect-which-one-is-pressed/#findComment-241607 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.