1
2<?php
3
4 if (!empty($_POST))
5 {
6 // Array of post values for each different form on your page.
7 $postNameArr = array('F1_Submit', 'F2_Submit', 'F3_Submit');
8
9 // Find all of the post identifiers within $_POST
10 $postIdentifierArr = array();
11
12 foreach ($postNameArr as $postName)
13 {
14 if (array_key_exists($postName, $_POST))
15 {
16 $postIdentifierArr[] = $postName;
17 }
18 }
19
20 // Only one form should be submitted at a time so we should have one
21 // post identifier. The die statements here are pretty harsh you may consider
22 // a warning rather than this.
23 if (count($postIdentifierArr) != 1)
24 {
25 count($postIdentifierArr) < 1 or
26 die("\$_POST contained more than one post identifier: " .
27 implode(" ", $postIdentifierArr));
28
29 // We have not died yet so we must have less than one.
30 die("\$_POST did not contain a known post identifier.");
31 }
32
33 switch ($postIdentifierArr[0])
34 {
35 case 'F1_Submit':
36 echo "Perform actual code for F1_Submit.";
37 break;
38
39 case 'Modify':
40 echo "Perform actual code for F2_Submit.";
41 break;
42
43 case 'Delete':
44 echo "Perform actual code for F3_Submit.";
45 break;
46 }
47}
48else // $_POST is empty.
49{
50 echo "Perform code for page without POST data. ";
51}
52?>
53
54
1
2There's an earlier note here about correctly referencing elements in $_POST which is accurate. $_POST is an associative array indexed by form element NAMES, not IDs. One way to think of it is like this: element "id=" is for CSS, while element "name=" is for PHP. If you are referring to your element ID in the POST array, it won't work. You must assign a name attribute to your element to reference it correctly in the POST array. These two attributes can be the same for simplicity, i.e.,
3<input type="text" id="txtForm" name="txtForm">...</input>
4
1
2I know it's a pretty basic thing but I had issues trying to access the $_POST variable on a form submission from my HTML page. It took me ages to work out and I couldn't find the help I needed in google. Hence this post.
3
4Make sure your input items have the NAME attribute. The id attribute is not enough! The name attribute on your input controls is what $_POST uses to index the data and therefore show the results.
5