Posted By

julioantuneztarin on 06/17/11


Tagged

php forms multiple


Versions (?)

Process multiple forms in a page


 / Published in: PHP
 

URL: http://www.php.net/manual/es/reserved.variables.post.php

  1. <?php
  2.  
  3. if (!empty($_POST))
  4. {
  5. // Array of post values for each different form on your page.
  6. $postNameArr = array('F1_Submit', 'F2_Submit', 'F3_Submit');
  7.  
  8. // Find all of the post identifiers within $_POST
  9. $postIdentifierArr = array();
  10.  
  11. foreach ($postNameArr as $postName)
  12. {
  13. if (array_key_exists($postName, $_POST))
  14. {
  15. $postIdentifierArr[] = $postName;
  16. }
  17. }
  18.  
  19. // Only one form should be submitted at a time so we should have one
  20. // post identifier. The die statements here are pretty harsh you may consider
  21. // a warning rather than this.
  22. if (count($postIdentifierArr) != 1)
  23. {
  24. count($postIdentifierArr) < 1 or
  25. die("\$_POST contained more than one post identifier: " .
  26. implode(" ", $postIdentifierArr));
  27.  
  28. // We have not died yet so we must have less than one.
  29. die("\$_POST did not contain a known post identifier.");
  30. }
  31.  
  32. switch ($postIdentifierArr[0])
  33. {
  34. case 'F1_Submit':
  35. echo "Perform actual code for F1_Submit.";
  36. break;
  37.  
  38. case 'Modify':
  39. echo "Perform actual code for F2_Submit.";
  40. break;
  41.  
  42. case 'Delete':
  43. echo "Perform actual code for F3_Submit.";
  44. break;
  45. }
  46. }
  47. else // $_POST is empty.
  48. {
  49. echo "Perform code for page without POST data. ";
  50. }
  51. ?>

Report this snippet  

You need to login to post a comment.