The other day I was looking for a solution to help me create a form without the hassle to check it again and again for missing arguments or miss-typing.
Because i couldn’t find any ready made solutions I created this little function that made my life less miserable.
I hope it could be useful to you too.
/*
$ewd_type - could be text, password, submit, hidden, textarea or select
$ewd_name - the name and id of the input filed
$ewd_value - input value default or returned by post
$ewd_size - the size of the input type text
$ewd_cols - number of colums for the textarea type field - default = 30
$ewd_rows - number of rows for the textarea type field - default = 6
*/
function ewd_make_form_field($ewd_type,$ewd_name,$ewd_value='',$ewd_size='',$ewd_cols='',$ewd_rows=''){
global $_POST;
$ewd_size = $ewd_size != "" ? $ewd_size : 30;
$ewd_cols = $ewd_cols != "" ? $ewd_cols : 30;
$ewd_rows = $ewd_rows != "" ? $ewd_rows : 6;
switch ($ewd_type) {
case "text":
echo "<input type='$ewd_type' name='$ewd_name' id='$ewd_name' size='$ewd_size' value='$ewd_value' onfocus='this.style.borderColor=\"#0072BC\";' onblur='this.style.borderColor=\"silver\";' />";
break;
case "password":
echo "<input type='$ewd_type' name='$ewd_name' id='$ewd_name' size='$ewd_size' value='$ewd_value' />";
break;
case "textarea":
echo "<textarea name='$ewd_name' id='$ewd_name' cols='$ewd_cols' rows='ewd_rows'>$ewd_value</textarea>";
break;
case "submit":
echo "<input type='$ewd_type' name='$ewd_name' id='$ewd_name' value='$ewd_value' />";
break;
case "hidden":
echo "<input type='$ewd_type' name='$ewd_name' id='$ewd_name' value='$ewd_value' />";
break;
case "select":
echo "<select name='$ewd_name' id='$ewd_name'>
<option value=''>Please Select</option>";
$ewd_y_value = explode(" ",$ewd_value);
foreach ($ewd_y_value as $ewd_word) {
echo "<option value='$ewd_word'";
if($ewd_word == trim($_POST[$ewd_name])){
echo " selected='selected'";
}
echo ">".$ewd_word."</option>";
}
echo "</select>";
break;
default:
echo "<input type='text' name='$ewd_name' id='$ewd_name' size='$ewd_size' value='$ewd_value' />";
}
}
Method for using the above function:
<form method="post" action="<?php $_SERVER['REQUEST_URI'];?>" name="x_form" id="x_form">
<table summary="some form">
<tr>
<td colspan="2" style="background-color:#f5f5f5; padding:3px;">
<b>About yourself</b>
</td>
</tr>
<tr>
<td>Name:</td>
<td><?php ewd_make_form_field('text','name',isset($_POST['name']) ? $_POST['name'] : '');?></td>
</tr>
<tr>
<td>Surname:</td>
<td><?php ewd_make_form_field('text','surname',isset($_POST['surname']) ? $_POST['surname'] : '');?></td>
</tr>
<tr>
<td>Email:</td>
<td><?php ewd_make_form_field('text','email',isset($_POST['email']) ? $_POST['email'] : '');?></td>
</tr>
<tr>
<td>Phone:</td>
<td><?php ewd_make_form_field('text','phone',isset($_POST['phone']) ? $_POST['phone'] : '');?></td>
</tr>
<tr>
<td>Type of Sale:</td>
<td><?php ewd_make_form_field('select','sale_type','Private Garage Both');?></td>
</tr>
<tr>
<td valign="top">Extra details:</td>
<td><?php ewd_make_form_field('textarea','additional',isset($_POST['additional']) ? $_POST['additional'] : '');?>(eg. Engine cc: 1.1 to 1.4)</td>
</tr>
<tr>
<td colspan="2" align="center">
<?php ewd_make_form_field('submit','submit','Send');?>
<?php ewd_make_form_field('hidden','country','Ireland');?>
</td>
</tr>
</table>
</form>