2

So I've been thinking about this for an hour and am interested in what the best way is to check which form has been submitted.

So lets say we've got formOne and formTwo that both submit to formsubmission.php and inside there I have deemed the most appropriate way to check for SUBMISSION is

if($_SERVER['REQUEST_METHOD'] == 'POST'){
//do stuff
}

But what is the best way to actually determine WHICH form has been submitted, formOne or Two?

I have read here What is the best way to identify which form has been submitted? and am wondering whether these are the only two ways to get around this.

I figured that just checking isset($_POST[formname]) would be bad due to a few reasons I read about elsewhere, but now I am starting to think that the whole idea of posting to the same .php file is just bad practice and there is no GOOD way to check which form has been submitted doing it this way.

Is using GET and submitting each different form to a seperate ID bad practice? if so - why?

So my question is, what is the best way to check WHICH form has been submitted, that is GOOD practice?

Thanks

PS also looked at hidden fields -> doesn't seem html worthy in terms of quality

Community
  • 1
  • 1
Jim
  • 482
  • 1
  • 5
  • 20
  • If you create site in a RESTful way then you won't have to worry as much about which form is submitted where. [Read more about REST here](http://stackoverflow.com/questions/671118/what-exactly-is-restful-programming) – Dan Sep 13 '14 at 13:02

3 Answers3

1

I would suggest you'd check the fields that you received if they don't have the exact same fields or have a hidden field which tells your php code which form it is, there is nothing wrong about that. However, the best option is to have separate urls for separate actions, regardless of which php file actually handles the submission in the end.

To do this you should look into urlrewrite and htaccess, which will allow you to turn a url like users/delete and users/add to myphpfile.php?action='delete' (meaning POST data is preserved) and in your php code look at the $_GET['action']value to decide which code to run.

Juan Cortés
  • 20,634
  • 8
  • 68
  • 91
1

The best would be to use a different name for each from submit input :

For formOne :

 <input type="submit" name="formOne" value="formOneValue" />

For formTwo :

 <input type="submit" name="formTwo" value="formTwoValue" />

Then in you're php file :

 if (!empty($_POST['formOne'])) {
     //do something here;
 }

 if (!empty($_POST['formTwo'])) {
     //do something here;
 }
Isaac
  • 983
  • 1
  • 7
  • 13
1

In my CMS I have more then three from that submit onto the same page.You just need to see which form has been submitted using your submit button name and value.Your request can either be a POST or a GET. Here's one example for two form that submit onto the same page. The form submit button name are different.

 <input type="submit" name="video" id="submit" value="Save">  -- first form 
 <input type="submit" name="album" id="submit" value="Save">  -- second form

 if(isset($_POST['video']) && $_POST['video']=='Save'){
    #code for first form
 }

if(isset($_POST['album']) && $_POST['album']=='Save'){
   #code for second form
}
user3107673
  • 423
  • 4
  • 9