One thing that I find myself doing continuously is creating forms. From forms that submit info into a database, to forms that send emails. I constantly am doing repetitive processes.
Most of the time I am using server side PHP code to create or validate part of the form. So I created a few server side functions that I have found help my process along. Two things that I seem to need often are radio lists and drop down options. I needed them to be as dynamic as possible. (To work with javascript, labels, css, validation, and database information)
I created two functions: one for radio buttons, and one for drop down options. I am going to list the entire functions and than explain the different aspects of them.
Radio List Function
function Fradio($a,$name,$c=FALSE,$o=FALSE){
//a=array(array('v'=>'value','l'=>'label','o'=>'extra/options/javascript'))
//name=radio form's name
//c=choice
//o=other values [true/false]
$x=0;//the count
if(is_array($a)){
if(!$o){//if no individual options for each input field, like javascript
foreach($a as $v){
$x=$x+1;
$selected = ($c == $v['v']) ? 'checked="checked"' : '';
$line .= ' <label for="'.$name.$x.'">
<input type="radio" name="'.$name.'" id="'.$name.$x.'" '.$selected.' value="'.$v['v'].'" />
'.$v['l'].'
</label>';
}
}else{
foreach($a as $v){
$selected = ($c == $v['v']) ? 'checked="checked"' : '';
$line .= ' <label for="'.$name.$x.'">
<input type="radio" name="'.$name.'" id="'.$name.$x.'" '.$selected.' value="'.$v['v'].'" '.$v['o'].' />
'.$v['l'].'
</label>';
}
}
return $line;
}
return false;
}
Drop Down Function
function Fdd($a,$name,$c=FALSE,$o=FALSE){
//a=array(array('v'=>'value','l'=>'label'))
//name=dropdown form's name
//c=choice
//o=other values [extra/options/javascript]
if(is_array($a)){
$line = '<select id="'.$name.'" name="'.$name.'" '.$o.'>';
foreach($a as $v){
$selected = ($c == $v['v']) ? 'selected="selected"' : '';
$line .= '<option value="'.$v['v'].'" '.$selected.' >'.$v['l'].'</option>';
}
$line .= '</select>';
return $line;
}
return false;
}
They work using arrays of information, I thought this would be the best way for me to create data and cycle through it. I will start by showing you a basic array of information, and what basic information the functions require.
To make this easier to read I’ve separated each array(choice) into a variable. You can easily create this array from a mysql result set.
$choice1 = array('v'=>'value1','l'=>'label1','o'=>'option1');
$choice2 = array('v'=>'value2','l'=>'label2','o'=>'option2');
$choice3 = array('v'=>'value3','l'=>'label3','o'=>'option3');
$array = array($choice2,$choice2,$choice3);
If you were trying to use mysql you could do something like this.
while($row = mysql_fetch_row($resultset)){
$array[] = array('v'=>$row["value"],'l'=>$row["label"],'o'=>$row["option"]);
}
Than you can store your radio list in a variable or simply display the function.
echo Fradio($array,'radioName','choice selected');
Now there are four variables that can be passed into this function and two of them are required. From left to right they are this: the array containing the values for the form tag(required), the name being used for the form tag(required), the choice selected(can be used for cms, or error correction), and lastly set this to true if any extra options are to be used(rarely needed, but allows for the use of javascript).
Both functions use the same format except on one part. Because of the way drop downs work, you cannot use javascript on each individual option tag(it won’t work correctly in all browsers, and doesn’t validate). Instead you must use it on the select tag. For the drop down function, the variable isn’t a boolean value, it’s the actual javascript. There is no need to put it in the array like the radio function. The radio function accepts a boolean value for that variable, and you actually put the javascript in the array along with the other radio button info.
Both functions will return false if the first variable is not an array. If you would like to comment or make suggestions feel free to post below. Feel free to use this, and I hope it helps.







One Comment
This is great info to know.