Jump to content

PHP POST help


c_pattle

Recommended Posts

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. 

Link to comment
Share on other sites

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>';

?>


Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.