Arrays & Forms & Script
| From: | Jeff Lacy | Date: | Wed, 29 Nov 2000 05:12:29 +0000 |
| Subject: | Arrays & Forms & Script | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-27769@lists.php.net to get a copy of this message | ||
Hello World,
Could someone please help me with one of my scripts? It is under development. Eventually, it
will take an array and produce a form. My problems are:
1.) formPhone takes values, like for values and other and puts 1 letter in each box.
2.) formName doesn't like me at all. I hope you see what I am trying to do.
I would also appreciate ANY advice anyone could give me. Thanks a whole bunch, everyone.
****************************test.php*********************************
<?php
$b = array (
array ("form", $PHP_SELF, "GET"),
array ("text", "first", "Jeffy", "10", "21",
"onMouseOver=\"window.status='Jeff is cool :-)'\"
onMouseOut=\"window.status=''\""),
array ("name", array ("first", "last"), "", "6",
"7"),
array ("phone", array ("1", "2", "3"), "",
"onMouseOver='window.status=\"Geometry is EVIL\";'")
);
require ('form.inc');
showForm ($b);
?>
*************************form.inc******************************
<?php
/**
This function will create an html form for you
It accepts an array:
Type [form]
1 Action
2 Method
3 Name
4 Other
[text]
1 Name
2 Value
3 Size
4 Maxlength
5 Other [Javascript Stuff, ...]
[name] // Takes an array for everything, but at least name
1 Names [first, middle, last, ...]
2 Values
3 Sizes
4 Maxlengths
5 Others [Javascript Stuff, ...]
[phone] // Takes an array for everything, but at least name
1 Names [area, 1, 2]
2 Values
3 Other [Javascript Stuff]
BUGS: Source Description
formName Limited to 5 names
Can't (nicely) pass $size value to formText when
$size isn't an array. Same for $maxlength.
formPhone Takes single value and spreads it over options and
values.
**/
function showForm ($a) {
for ($i=0; $i <= sizeof ($a); $i++) {
if ($a[$i][0] == "form") {
formForm ($a[$i][1], $a[$i][2], $a[$i][3], $a[$i][4]);
}
if ($a[$i][0]=="text") {
formText ($a[$i][1], $a[$i][2], $a[$i][3], $a[$i][4], $a[$i][5]);
}
if ($a[$i][0]=="name") {
formName ($a[$i][1], $a[$i][2], $a[$i][3], $a[$i][4], $a[$i][5]);
}
if ($a[$i][0]=="phone") {
formPhone ($a[$i][1], $a[$i][2], $a[$i][3]);
}
}
}
function formForm ($action = '$PHP_SELF', $method = 'POST', $name =
'', $other = '') {
echo "<form action=\"$action\" method=\"$method\"";
if ($name) { echo " name=\"$name\""; }
if ($other) { echo " $other"; }
echo ">\n<br>";
}
function formText ($name, $value = '', $size = '20', $maxlength = '',
$other = '', $caller = '') {
echo "<input type=\"text\" name=\"$name\" size=\"$size\"
value=\"$value\"";
if ( ($size <= $maxlength) && $maxlength) { echo "
maxlength=\"$maxlength\""; }
if ($other) { echo " $other"; }
echo ">";
if (!$caller) { echo "\n<br>"; }
}
function formName ($name, $value = '', $size = '20', $maxlength = '',
$other = '') {
for ($i=0; $i < sizeof($name); $i++) {
if (!is_array($size)) { $size = array ($size[0], $size[0], $size[0], $size[0], $size[0]); }
if (!is_array($maxlength)) { $maxlength = array ($maxlength[0], $maxlength[0], $maxlength[0],
$maxlength[0], $maxlength[0]); }
formText ($name[$i], $value[$i], $size[$i], $maxlength[$i], $other[$i], "formName");
if ($i != (sizeof($name)-1) ) { echo " "; }
else { echo "<br>\n"; }
}
}
function formPhone ($name, $value = '', $other = '') {
for ($i=0; $i < sizeof($name); $i++) {
if (sizeof($name) == 3) { $size = array ("3", "3", "4"); }
else { $size = array ("3", "4"); }
if (sizeof($name) == 3 && $i == 0) { echo "("; }
if (sizeof($name) == 3 && $i == 1) { echo ")"; }
if (sizeof($name) == 3 && $i == 2) { echo "-"; }
if (sizeof($name) == 2 && $i == 1) { echo "-"; }
formText ($name[$i], $value[$i], $size[$i], $size[$i], $other[$i], "formName");
if (sizeof($name) == 3 && $i == 2) { echo "<br>\n"; }
if (sizeof($name) == 2 && $i == 1) { echo "<br>\n"; }
}
}
?>