c_pattle Posted July 16, 2011 Share Posted July 16, 2011 I have a form that has lots of fields and lots of submit buttons. The PHP that I am using at the moment if one of these submit buttons has been pressed is if(($_POST['submit1']) || ($_POST['submit2']) || ($_POST['submit3'])..... As there are about 30 of these submit buttons it means that there is a lot of code. Is there an easier way to do this? Is there a way to have a condition such you can check if a submit button between "submit1" and "submit30" has been pressed? Thanks for any help. Quote Link to comment https://forums.phpfreaks.com/topic/242154-php-post-help/ Share on other sites More sharing options...
PFMaBiSmAd Posted July 16, 2011 Share Posted July 16, 2011 How about using the same name="submit" for all of them, but using a different value="..." to indicate which one was submitted? Quote Link to comment https://forums.phpfreaks.com/topic/242154-php-post-help/#findComment-1243582 Share on other sites More sharing options...
jcbones Posted July 16, 2011 Share Posted July 16, 2011 You could use something along the lines of: <?php $_POST['ubmit10'] = true; $_POST['submit23'] = true; $_POST['submit40'] = true; $_POST['submit1'] = true; $keyExists = false; $postKeys = array_keys($_POST); for($i = 1; $i < 31; $i++) { if(in_array('submit'.$i,$postKeys)) { $keyExists = true; $keyCalled = 'submit' . $i; break; } } if($keyExists) { echo 'YES<br />' . $keyCalled . ' does exist!<br />'; } else { echo 'NO<br />No keys present<br />'; } echo '<pre>' . print_r($_POST,true) . '</pre>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/242154-php-post-help/#findComment-1243585 Share on other sites More sharing options...
c_pattle Posted July 17, 2011 Author Share Posted July 17, 2011 Thanks for the advice! Quote Link to comment https://forums.phpfreaks.com/topic/242154-php-post-help/#findComment-1243750 Share on other sites More sharing options...
c_pattle Posted July 17, 2011 Author Share Posted July 17, 2011 Also one other question. Is it "bad" practice to have more than one submit button in the same form? Quote Link to comment https://forums.phpfreaks.com/topic/242154-php-post-help/#findComment-1243751 Share on other sites More sharing options...
PFMaBiSmAd Posted July 17, 2011 Share Posted July 17, 2011 It all depends on what it is you are tying to accomplish. If you have a form where you need to allow the visitor to choose between different operations for the data that was entered, then yes, there's nothing wrong with having more than one submit button in a form. Quote Link to comment https://forums.phpfreaks.com/topic/242154-php-post-help/#findComment-1243753 Share on other sites More sharing options...
teynon Posted July 17, 2011 Share Posted July 17, 2011 The problem with relying on submit buttons is that the user can submit the form using the enter key. If they do this, none of the form buttons will be submitted. The way I get around this is to use a hidden input form that verifies the form is submitted: <input type="hidden" name="form_submitted" value="1"> As for when you need to decide what you want to do with the information, you can do one of the following options: Let it submit. If they pushed enter, just send a note saying they need to pick an option next time. Use JavaScript to override the enter button. Or have the user choose what they are doing before they get to the form (PREFERRED), making it non-dependent on what form button they push. Quote Link to comment https://forums.phpfreaks.com/topic/242154-php-post-help/#findComment-1243754 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.