1 <?
2
3 #The following uses a form with a hidden "submited" value to see if you're
4 #accessing the page with or without submitting the form.
5 #further is checks the contents of the form as valid before processing it
6 #lastly, if it has any troubles it regenerates the form with error messages
7
8 #if you don't understand preg_match, you must try it.
9 #look at:
10 #http://www.php.net/manual/en/function.preg-match.php
11 #and:
12 #http://www.php.net/manual/en/pcre.pattern.syntax.php#regexp.reference
13 # or in unix `man perlre` (note there are some php to perl differences.
14
15 function print_form() {
16 #This $_SERVER and _POST stuff works in most phps, but PHP4.2 requires it.
17 $self=$_SERVER['PHP_SELF']; #instead of $PHP_SELF
18 $phone=array_key_exists('phone',$_POST)?$_POST['phone']:'';
19 $name =array_key_exists('name',$_POST)?$_POST['name']:'';
20 echo <<<END
21 <form method="post" action="$self" name="theForm">
22 <input type="hidden" name="submited" value="aye laddie aye">
23 name: <input type="text" name="name" size="30" maxsize="30" value="$name">
24 <br>
25 phone: <input type="text" name="phone" size="30" maxsize="30" value="$phone">
26 <input type="submit" value="go go go">
27 </form>
28 END;
29 }
30 echo "<html><head><title>test</title></head><body>\n";
31 #init to no problem and no error messages.
32 $problem=false;
33 $error_msgs = "";
34 #error_msgs can grow to contain many messages by '.=' adding to it.
35 # this used to work: if($_POST['submited']){ #but now it throws a "Notice"
36 if(array_key_exists('submited', $_POST)){
37 #the user came here by submiting the form.
38 #here we check the variables, and if they're bad we output the form
39 #again, defaulting variable values to those we know.
40 #otherwise we're happy and process the form with a result page.
41 if(array_key_exists('name',$_POST) && !$_POST['name']) {
42 #name was blank
43 $problem=true;
44 $error_msgs .= "You must fill out the name field.<br>\n";
45 } else {
46 #name is not blank.
47 if(!preg_match("/\w+\s+\w+/", $_POST['name'])) {
48 #name did not match at least 2 words
49 $problem=true;
50 $error_msgs .= "You must fill out the name field with at least a first and last name.<br>\n";
51 }
52 }
53
54 if(array_key_exists('phone',$_POST) && !$_POST['phone']) {
55 #there is a problem with phone (its blank)
56 $problem=true;
57 $error_msgs .= "You must fill out the phone field.<br>\n";
58 } else {
59 #phone is not blank,
60 if(!preg_match("/^[0-9+\-x() ]*$/", $_POST['phone'])) {
61 #phone did not (!) match a string made only of 0123456789-+x() and space
62 $problem=true;
63 $error_msgs .= "You must fill out the phone field using only: 0-9 '-' '+' 'x' '(' or ')'.<br>\n";
64 #a better phone matcher might look like this:
65 # /^(\+\d{1,5})?(\(\d{1,4}\))\d{3,4}-?\d{3,4}(x\d{1,7})?$/
66 # form: opt"+<country>" . "(<area>)<number>" . opt"x<extention>"
67 # but it may not work for all countries.
68 #allows:
69 # "+1(508)579-5467" "(508)5795467" "+971(4)440-592" "(508)579-5467x16"
70 #disalows:
71 # "579-5467" "(+1)..." "(1)12-12" "(508)579-5443-5467" "(508)5795467ext4"
72 #if(!preg_match("/^(\+\d{1,5})?(\(\d{1,4}\))\d{3,4}-?\d{3,4}(x\d{1,7})?$/", $_POST['phone'])) {
73 }
74 }
75
76 if(!$problem) {
77 echo "GOOD GOOD GOOD\n";
78 } else {
79 echo $error_msgs;
80 print_form();
81 }
82 } else {
83 #the user came here without submiting the form, so we give him the form.
84 print_form();
85 }
86 echo "<p>Want to see the <a href=\"phpform.php.html\">source</a>?</p>\n";
87 echo "</body></html>";
88 ?>