c_pattle Posted October 4, 2010 Share Posted October 4, 2010 I was wondering if you can have a form that has different actions depending on what submit button is pressed. I have two button and with one I was to use <form action="<?php echo ( $_SERVER['PHP_SELF'] ); ?>" method="post"> and with the other I want to use <form action="preview.php" method="post"> I was to do this because I want to user to be able to preview their content before submitting. I want the preview page to use the post variables as the value of the input fields so the user doesn't have to input their data again. However because the form doesn't redirect the user to the preview page I have to save all the post variables as session variables. Quote Link to comment https://forums.phpfreaks.com/topic/215162-help-with-submit-buttons/ Share on other sites More sharing options...
BlueSkyIS Posted October 4, 2010 Share Posted October 4, 2010 i usually give each submit button a different name (naturally), and check which $_POST['button_name'] was submitted, then continue acting from there; either header() to a page or (better, imo) split logic based on the button clicked. Quote Link to comment https://forums.phpfreaks.com/topic/215162-help-with-submit-buttons/#findComment-1119086 Share on other sites More sharing options...
rwwd Posted October 4, 2010 Share Posted October 4, 2010 I was wondering if you can have a form that has different actions depending on what submit button is pressed. I have two button and with one I was to use Most certainly can! in the receiver script, you would just do this:- if (isset($_POST['submit']) && ($_POST['submit'] == "First Button")){//call this button what you like! //process form 1 'First Button' here } else{ //redirect back to form, processing error header("location: yourfilenamehere"); } if (isset($_POST['submit']) && ($_POST['submit'] == "Second Button")){//call this button what you like! //process form 2 'Second Button' here } else{ //redirect back to form, processing error header("location: yourfilenamehere"); } There are simpler ways to action this, but it all down to this next part really! <form action="yourfilename" method="POST"> <div> CONTAINER FOR ALTERNATING CONTENT can be ajax/js etc <input type="submit" name="submit" value="First Button"><--this changes depending what div is visible </div> </form> Just depends of what div is set as visible as to what submit button gets made visible... this is pseudo code, and only offers the concept of how to construct this, there are other ways, but this is just the first that springs to mind, just google "div switcher" to see the options available, I use this similar sort of thing for doing single/multiple uploads, and it works a treat.. Rw Quote Link to comment https://forums.phpfreaks.com/topic/215162-help-with-submit-buttons/#findComment-1119087 Share on other sites More sharing options...
Pikachu2000 Posted October 4, 2010 Share Posted October 4, 2010 Don't use $_SERVER['PHP_SELF'] as a form action. It presents an XSS vulnerability. Either leave the action attribute blank, or name the file if you want to submit a form to itself. Quote Link to comment https://forums.phpfreaks.com/topic/215162-help-with-submit-buttons/#findComment-1119093 Share on other sites More sharing options...
BlueSkyIS Posted October 4, 2010 Share Posted October 4, 2010 Don't use $_SERVER['PHP_SELF'] as a form action. It presents an XSS vulnerability. Either leave the action attribute blank, or name the file if you want to submit a form to itself. I never new that, and I use PHP_SELF all the time. I will change my habit. Quote Link to comment https://forums.phpfreaks.com/topic/215162-help-with-submit-buttons/#findComment-1119094 Share on other sites More sharing options...
c_pattle Posted October 4, 2010 Author Share Posted October 4, 2010 thanks for all the help. Although if you use "header" to redirect to a different page don't the $_POST variables clear themselves? Quote Link to comment https://forums.phpfreaks.com/topic/215162-help-with-submit-buttons/#findComment-1119095 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.