PHP Creating and processing forms with the help of arrays

Jan 14, 2009 Author: City Hall

The example consists of two files:

1. postForm.php
2. arrayFunction.php
The contents of the first file:

CODE

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<html>
 <
head><title></title></head>
 <
body>
<?
php
  include
('arrFunction.php');
 
class clsForm{
     
private $defForm = array();
     
private $thispage;
     
private $mode;
     
public function __construct(){
           
$this->thispage = $_SERVER['PHP_SELF'];
           
$this->mode = $_POST['mode'];
           
$this->defForm = array(
             
"userName"=>""
              ,
"userFamily"=>""
              ,
"loginName"=>""
              ,
"email"=>""
              ,
"pwd"=>""
              ,
"cpwd"=>""
           
);
            
if(!isset($this->mode))$this->mode = "defForm";
           
switch($this->mode){
             
case "postForm":
                
$this->getPostVariables();
               
break;
               
default:
             
case "defForm":
                 
$this->displayRegisterForm();
                 
break;
           
}
      }
// construct()
     
     
private function getPostVariables(){
          
printArrays($_POST);
     
} //  getPostVariables()
     
     
private function displayRegisterForm(){
         
$html =
           
"<form name='lForm' method='post' action=\"".$this->thispage."\">\n ".
           
"     <input type='hidden' name='mode' value='postForm'>\n".
           
"   <table align='center' border='1' width='300'>\n".
           
"     <tr>\n".
           
"       <td>User Name: </td>\n".
           
"       <td><input type='text' name='defForm[userName]' value=\"".$this->defForm["userName"]."\" size='25'></td>\n".
           
"     </tr>".
           
"     <tr>\n".
           
"       <td>User Family: </td>\n".
           
"       <td><input type='text' name='defForm[userFamily]' value=\"".$this->defForm["userFamily"]."\" size='25'></td>\n".
           
"     </tr>".
           
"     <tr>\n".
           
"       <td>Login Name: </td>\n".
           
"       <td><input type='text' name='defForm[loginName]' value=\"".$this->defForm["loginName"]."\" size='25'></td>\n".
           
"     </tr>".
           
"     <tr>\n".
           
"       <td>Email: </td>\n".
           
"       <td><input type='text' name='defForm[email]' value=\"".$this->defForm["email"]."\" size='25'></td>\n".
           
"     </tr>".
           
"     <tr>\n".
           
"       <td>Password: </td>\n".
           
"       <td><input type='password' name='defForm[pwd]' value=\"".$this->defForm["pwd"]."\" size='25'></td>\n".
           
"     </tr>".
           
"     <tr>\n".
           
"       <td>Reply Password: </td>\n".
           
"       <td><input type='password' name='defForm[cpwd]' value=\"".$this->defForm["cpwd"]."\" size='25'></td>\n".
           
"     </tr>".
           
"     <tr>\n".
           
"       <td colspan='2' align='right'><input  type='submit' value='Send Form'></td>\n".
           
"     </tr>".
           
"   </table>".
           
"</form>\n";
           
print $html;
     
//displayRegisterForm()
     
  }
//  clsForm
 
 
$pb = &new clsForm();
?>
</
body>
</
html>

Second file :
 CODE

1
2
3
4
5
6
7
8
9
10
<?php
  function
printArrays($arr){
     
foreach($arr as $key=>$val){ 
       
print "<table border=1>\n";
       
print "<tr><td><b>Keys</b></td><td><b>Values</b></td></tr>\n";
       
print "<tr><td> ".$key."</td><td> ".(is_array($val)? printArrays($val) :$val)."</td></tr>\n";
       
print "</table>\n";
       
}
  }  
// printArrays()
?>

In the first class have clsForm file which contains two methods for each display format

displayRegisterForm () which will be shown by default when the page is loaded. Second method getPostVariables () will print in the form of table postnatite arrays. Created a single function printArrays () which will print array. It is the second  file. But calls to the method getPostVariables (). In class we have several pre-defined fields notice attention taken private $ defForm = array (); Here you define an empty array that will use to name field in form. in the class constructor call empty array, and set default values, I am left all empty.

Explanation of the field: <input type='text' name='defForm[userName]' value=\"".$this-> defForm [ "userName"]. "\" Size ='25 '>. This creates a text field named defForm [userName] which corresponds to the array defined in the class constructor, and some "userName" is the key of the array. In PHP array containing key => value. In the value = \ "". $ This-> defForm [ "userName"]. "\" Set stoynotta field. Because massif in the initial call is an empty box will not contain any value. But if there is a set of solid value or something posted will be set as the value of the field.


views 4842
  1. Add New Comment