Ordinary_Shepp Posted April 3, 2014 Share Posted April 3, 2014 How can I get two submit button on one form with two different action? <?php require_once 'func.php'; connect() ; ?> <!DOCTYPE html> <html> <head> </head> <body> <header> <h1 align='center'>student</h1></header> <form method="post" action="process.php" enctype="multipart/form-data"> <table> <tr> <td> </td> <td><input type="submit" value="Submit"></td></tr> </table> </form> </body> </html> Here is a code segment. I want to get two button "submit" and "go back" which should take me to the "process.php" and "index.php" . Thanks in advance for your help. And is there any way to call a php function by clicking a submit button ? Link to comment https://forums.phpfreaks.com/topic/287500-how-to-two-submit-button-on-one-form-with-two-different-action/ Share on other sites More sharing options...
iarp Posted April 3, 2014 Share Posted April 3, 2014 When you click on a button in a form, the ID of the button that is pressed is passed. So you could just check for isset($_POST['submit']) or isset($_POST['goback']) Link to comment https://forums.phpfreaks.com/topic/287500-how-to-two-submit-button-on-one-form-with-two-different-action/#findComment-1474853 Share on other sites More sharing options...
ginerjm Posted April 3, 2014 Share Posted April 3, 2014 Not to confuse the issue, but when a submit button is passed to the script it is not the "ID" that you look for but the "name" that you look for and in order to distinguish between multiple buttons with the same name you look for the "value". ex. <form method="POST"> <input type='submit' name='btn' value="Submit"> <input type='submit' name='btn' value="Cancel"> </form> In you php you would then do: if ( !isset($_POST['submit'])) { (handle no submit yet) } // got a click from the form $btn = $_POST['submit']; if ($btn == "Submit") { ( do the submit logic) } if ($btn == "Cancel") { (do the cancel logic) } Link to comment https://forums.phpfreaks.com/topic/287500-how-to-two-submit-button-on-one-form-with-two-different-action/#findComment-1474855 Share on other sites More sharing options...
ginerjm Posted April 4, 2014 Share Posted April 4, 2014 If anyone is still reading this I have to correct my code above. In the PHP code one should be checking $_POST['btn'] and NOT $_POST['submit']. Don't know what I was thinking when I dashed off that response. Two major errors in one hour today. Must need a rest or something...... Link to comment https://forums.phpfreaks.com/topic/287500-how-to-two-submit-button-on-one-form-with-two-different-action/#findComment-1474908 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.