MetalSmith Posted July 26, 2009 Share Posted July 26, 2009 Hello, Is there anyway to post different forms based on the submit button? I want to be able to click a submit button but have it post to a different php script based on the button pushed. I have this which works ok but I need more submit buttons within each form. </head> <body> <?php ?> <form action = "analysis-1.php" method = "post"> <input type="submit" name="submit" value="Analysis-1" /> </form> <form action = "analysis-2.php" method = "post"> <input type="submit" name="submit" value="Analysis-2" /> </form> </body> </html> EDIT: Please use [code][/code] tags when posting code on the forums, and use descriptive topic subjects. Quote Link to comment https://forums.phpfreaks.com/topic/167496-how-to-run-different-code-based-on-which-button-is-clicked/ Share on other sites More sharing options...
vineld Posted July 26, 2009 Share Posted July 26, 2009 You may have several submit buttons post to the same php file and then do differently depending on which one was used if that will work for you? Javascript and buttons will also do the job. It all depends on what you wish to accomplish, really. It's difficult for us to offer the best solution without knowing more. Quote Link to comment https://forums.phpfreaks.com/topic/167496-how-to-run-different-code-based-on-which-button-is-clicked/#findComment-883229 Share on other sites More sharing options...
zq29 Posted July 26, 2009 Share Posted July 26, 2009 You could name each button differently and use PHP to check which one was pressed: <?php if(isset($_POST['button_1'])) { //do something } elseif(isset($_POST['button_2'])) { //do something else } elseif(isset($_POST['button_3'])) { //do another thing } ?> <form name="foo" action="" method="post"> <input type="submit" name="button_1" value="Button 1" /> <input type="submit" name="button_2" value="Button 2" /> <input type="submit" name="button_3" value="Button 3" /> </form> Quote Link to comment https://forums.phpfreaks.com/topic/167496-how-to-run-different-code-based-on-which-button-is-clicked/#findComment-883230 Share on other sites More sharing options...
MetalSmith Posted July 26, 2009 Author Share Posted July 26, 2009 Well my thinking is that I have several php scripts that run certain reports. I was thinking the best way to do it was to have a submit button pushed to run a certain php script. Like by pushing "phone report" analyzes our phone usage. Then another would be "video conference" analyzes our video conference usage. So each button is attached to a php script. Maybe the way you posted would work. I need to think about it more. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/167496-how-to-run-different-code-based-on-which-button-is-clicked/#findComment-883480 Share on other sites More sharing options...
blueman378 Posted July 27, 2009 Share Posted July 27, 2009 so your using the buttons as links? are there any actual inputs on the form? if not you *could* put each button in its own form element. though id probably suggest you use JavaScript on the buttons to change the forms action Quote Link to comment https://forums.phpfreaks.com/topic/167496-how-to-run-different-code-based-on-which-button-is-clicked/#findComment-883593 Share on other sites More sharing options...
vineld Posted July 27, 2009 Share Posted July 27, 2009 Are there actually any forms? If not then using them for simple links is not really good semantics. Quote Link to comment https://forums.phpfreaks.com/topic/167496-how-to-run-different-code-based-on-which-button-is-clicked/#findComment-883614 Share on other sites More sharing options...
MetalSmith Posted July 27, 2009 Author Share Posted July 27, 2009 This is what I have very simple. If I can do this within one form that would give me some flexability to position my submit buttons. Otherwise I can't group them together. I would say this is more like links than an actual form. Its not gathering any data from a user. It just takes the persons uploaded file and analyzes it. <body> <p style="text-align: center;"> <img src="logo.jpg" width="192" height="56" border="0" /> </p> <hr align="center" width="100%" size="0" noshade /> <br /> <?php if (move_uploaded_file ($_FILES['thefile']['tmp_name'], "{$_FILES['thefile']['name']}")) { print '<p style="text-align: center;"> <font size="+2">Your file was successfully uploaded.</font> </p>'; } else { print '<p style="text-align: center;">Your file could not be uploaded because: <b>'; switch ($_FILES['thefile']['error']) { case 1: print 'The file exceeds the upload_max_filesize setting in php.ini'; break; case 2: print 'The file exceeds the MAX_FILE_SIZE setting in the HTML form'; break; case 3: print 'The file was only partially uploaded'; break; case 4: print 'No file was uploaded'; break; } print '</b>.</p>'; } ?> <form action = "analysis-1.php" method = "post"> <input type="submit" name="submit" value= "Analysis-1" /> </form> <form action = "analysis-2.php" method = "post"> <input type="submit" name="submit" value= Analysis-2" /> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/167496-how-to-run-different-code-based-on-which-button-is-clicked/#findComment-883623 Share on other sites More sharing options...
vineld Posted July 27, 2009 Share Posted July 27, 2009 Ok, well, since the user is uploading files there needs to be a form. Here's what seems to be the best solution: 1. Use one single form with several differently named submit buttons 2. Check which submit button was used in the target file 3. Do the analysis depending on 2. Setting up functions for the various analysis methods seems like a good idea, if they do not already exist. You need to add enctype to your form but maybe you already know that? Quote Link to comment https://forums.phpfreaks.com/topic/167496-how-to-run-different-code-based-on-which-button-is-clicked/#findComment-883625 Share on other sites More sharing options...
MetalSmith Posted July 28, 2009 Author Share Posted July 28, 2009 Yea I have this in there <form action = "process-log.php" enctype = "multipart/form-data" method = "post"> Thanks for all the help!! I will try your method!! Quote Link to comment https://forums.phpfreaks.com/topic/167496-how-to-run-different-code-based-on-which-button-is-clicked/#findComment-884478 Share on other sites More sharing options...
slapdashwebdesigner Posted July 28, 2009 Share Posted July 28, 2009 You could name each button differently and use PHP to check which one was pressed: <?php if(isset($_POST['button_1'])) { //do something } elseif(isset($_POST['button_2'])) { //do something else } elseif(isset($_POST['button_3'])) { //do another thing } ?> <form name="foo" action="" method="post"> <input type="submit" name="button_1" value="Button 1" /> <input type="submit" name="button_2" value="Button 2" /> <input type="submit" name="button_3" value="Button 3" /> </form> instead of this i would use a switch like this <?php switch ($_POST['submit']){ case 'Button 1': include('file1.php'); break; case 'Button 2': include('file2.php'); break; case 'Button 3': include('file3.php'); break; } ?> <form name="foo" action="" method="post"> <input type="file" /> <input type="submit" name="submit" value="Button 1" /> <input type="submit" name="submit" value="Button 2" /> <input type="submit" name="submit" value="Button 3" /> </form> just my preference, include the file that you want to handle that request Quote Link to comment https://forums.phpfreaks.com/topic/167496-how-to-run-different-code-based-on-which-button-is-clicked/#findComment-884488 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.