|
How many times that we have needed to integrate code PHP with forms elements, have we obtained a miscellany of HTML and tags <? php?> diminishing the legibility of the code or doing that we have to write more lines of the account?
With these simple but useful functions we will be able without problems to locate fields to the forms that we design without so many headaches.
The functions that are explained in this article are in the following direction http://www.distintiva.com/jose/_perf_form/perf_form.zip. The code can be changed, improved and distributed freely. It has been programmed completely by Jose Carlos García of Distinctive Solutions (www.distintiva.com).
Let's start with the most difficult example and the causer of whom he had to programme these functions.
<SELECT>
To create selection elements <select> </select>
With php it is very habitual to work with arrays, be already with information that it provides to us a consultation SQL or simple values.
Let's see an example in which we want to prove to be a <select> so that the user selects his age status:
<select yam = "age">
<option value=0> I Selected </option>
<option value=1> Between 0-18 </option>
<option value=2> Between 19-30 </option>
<option value=3> Between 31-50 </option>
<option value=4> More than 50 </option>
<select/>
This is complicated when it is necessary to do it of dynamic form since it is ready can change, to avoid to have to change the HTML “to hair” since we need to construct this HTML fragment with a ringlet, etc.
It is complicated moreover when an option must be presorted.
The function that will save us the life is:
frm_select ($name, $arr_txt, $arr_vals, $default = ", $extra_tag =")
$yam = Name of the element of the form
$arr_txt = Array with the texts to be showed
$arr_vals = Array with the values associated with every text
$default = [optional] if the value is indicated the above mentioned option will turn out to be presorted
$extra_tag = [optional] if we need to include additional information to the select, as for example $extra_tag = "class=cssazul" or $extra_tag = ”onChange=alert ()”
Example:
$arr_txt=array (‘Spain‘, 'portugal', 'francia');
$arr_vals=array (IT 'IS', ‘PT‘, FR’);
<? =frm_select (‘countries‘, $arr_txt, $arr_vals)?>
Thus any modification is necessary only to do it in the arrays without altering the visual part.
Sometimes it is suitable to preserve the value of a field of the form between calls or posts of the same one for example when we are validating earnings and it is necessary to return to the form so that it refills some required field. To do this of simple form we have only to use the parameter $default of the following form:
<? =frm_select (‘countries‘, $arr_txt, $arr_vals, $_POST ['countries])?> (or $_GET depending the method that we use in our form)
<SELECT> (type lists with several visible elements)
It works exactly just as the previous one but in this case we have the typical list of selection with scroll showing X elements.
The function is:
frm_list ($name, $size, $arr_txt, $arr_vals, $default = ", $extra_tag =") In this case the new parameter is $size that it indicates how many elements will be showed visibly in the list.
<SELECT> (type lists with several visible elements and with multiselection)
Just as the previous example but we allow the user to select one or several elements of the list with the CTRL+Click or SHIFT+Click
The function is:
frm_list_multi ($name, $size, $arr_txt, $arr_vals, $default = ", $extra_tag =") ... And also we have corresponding functions for other form elements making use of the aptitude to support the value between posts.
<CHECKBOX>
It is not necessary to say so that they serve these elements. The function is:
frm_check ($name, $ck_val, $var_in = ", $extra_tag =")
$yam = Name of the field
$ck_val = Value that will be sent when it is selected
$var_in = [optional] It Works like $default and it will allow to appear checkeado
Example:
It proves to be a checkbox that stays selected between posts of the form
<? = frm_check (‘smoker‘, 'YES', $_POST [‘smoker‘])?>
<RADIO>
In this case to show radiobuttons it is done exactly like the previous case but with the following function:
frm_radio ($name, $val, $var_in = ", $extra_tag =") <UNPUTT>
It allows to show text pictures with the advantages that I am offering in all the functions and for it it is necessary to use:
frm_text ($name, $val, $size, $max_length, $extra_tag = ")
$yam = Name of the field
$val = Value or text that will appear (it works like $default of other functions)
$size = Size of the field of text
$max_length = authorized maximum Length
<PASSWORD>
The case is exactly like the previous one but this time conceals the text with asterisks.
frm_password ($name, $val, $size, $max_length, $extra_tag = ")
Jose Carlos García
http://www.webestilo.com/php/articulo.phtml?art=48 |