Language:

Forms in PHP

9 February 2011 | In Web | 5 thousand views | By

Click & share: 


Uma das main utilizações do PHP, is to validate and store the information entered on forms. I.e., a user arrives at a certain page, populates a form, and when does the “submit” This information must be
processed by something. O PHP leva uma great vantagem neste aspecto, about CGI ’ s in C and Perl standards, not working in one way as transparent as the PHP.

When it is done a submit a form to a PHP script, PHP is capable of understanding what is sent, and transform um formulário num conjunto de variáveis com a informação introduzida. For example, If a user
populates a text box with your name, After the form submission, is there a variable with the name of the, containing information entered by the user. Example of a script “form.php”:

<form action="verifica.php">
Introduza o seu nome: <input type=txt name=nome><br>
<input type=submit>
</form>

When the form of the “form.php” is submitted to the verifica.php, is now available a variable $nome, with the information entered by the user.

Let's test:

<?
echo "O nome do utilizador é $nome <br>";
?>

We record the script with the name “verifica.php”, and we run our form.php.
We introduced there the name, and when we make the submit, the verifica.php sends the output:

O nome do utilizador é [Dragon]

Types of variables created by the forms:

Depending on the type of input that we have in our form, the variable that stores the information has different formats. For example, While a text box, creates a string with the name of the, and the value
introduced, for other tipos de input, the result is different. Step to list the various situations.

text - Creates a variable with the name of the. This variable is a string, and contains the information introduced in the box.

checkbox – Creates a variable with the name of the checkbox. This variable contains the values “on” or “” (empty) Depending on the checkbox is active or not.

radio – Creates a variable with the name of the radio. This variable contains the value of the selected radio.

select – Creates a variable with the name of the select. Contains the value of the OPTION selected.

textarea – Creates a variable with the name of the textarea. Contains the value entered in the same.

Example of the types of input:

<form action=verifica.php>
Caixa de texto: <input type=text name=text_name><br><br>

Check Box<input type=checkbox name=checkbox_name><br><br>

Radio:<br>
Valor 1<input type=radio name=radio_name value=valor1><br>
Valor 2<input type=radio name=radio_name value=valor2><br><br>

Select:<br>
<select name=select_name>
<option value=opcao1>Opção 1</option>
<option value=opcao2>Opção 2</option>
</select><br><br>

Area de texto:<br>
<textarea name=textarea_name></textarea><br>

<input type=submit>
</form>

AND now o nosso verifica.php:

<?
echo "Text: $text_name <br>";
echo "CheckBox: $checkbox_name <br>";
echo "Radio: $radio_name <br>";
echo "Select: $select_name <br>";
echo "Textarea: $textarea_name <br>";
?>

After you realize the multiple choice options, deinputs of various types to a form, Let's exercise:

Exercise 8:

<html>
<body>
<form action=verifica.php>
Texto: <input type=text name=texto><br><br>
Tipo de <Hx>: <select name=tamanho_hx>
<option value="1">H1</option>
<option value="2">H2</option>
<option value="3">H3</option>
<option value="4">H4</option>
</select><br><br>
Texto fica em Italico? <input type=checkbox name=italico checked><br><br>
Tipo de alinhamento:<br>
Esquerda<input type=radio name=alinhamento value="esquerda"><br>
Centro<input type=radio name=alinhamento value="centro"><br>
Direita<input type=radio name=alinhamento value="direita"><br><br>
Cor:<br>
<select name=cor>
<option value="#FF0000">Vermelho</option>
<option value="#00FF00">Verde</option>
<option value="#0000FF">Azul</option>
</select><br><br>
Numero de vezes: <input type=text name=vezes maxlength=2 size=3><br><br>
<input type=submit>
</form>
</body>
</html>

—————————————————————————–

Given this form, and after carefully analisares HTML, draws up a PHP script that processes the form and submits the appropriate results:

Type the text that you enter in the box “Text” with H1 type, H2, H3, or H4.
The text may or may not be in italics, and alignment should be left, Center, or right, with the color that we chose. Finally, should be written depending on the number of times introduced by the user. The number of times may not be less than 1 nor more than 30, and in each line should contain the number of the row to be written.

Possible Results:

Example1:
<h1 align=center><font color=#FF000><i>1- Exemplo 1!!</i></font></h1>

Example 2:
<h4 align=right><font color=#000FF>1- Exemplo 2!!</font></h4>
<h4 align=right><font color=#000FF>2- Exemplo 2!!</font></h4>
<h4 align=right><font color=#000FF>3- Exemplo 2!!</font></h4>

Summary:
PHP uses the data from a form in a transparent way. The fields of a form introduced, is available in the form of PHP script variables with the corresponding values.

In Ivaiporã-PR, Computer engineer, Workgroup Administrator Tips in General. Passionate about technology and Informatics.



A bit about us

    The Group generally appeared in Tips 2007 from innovative ideas on troubleshooting problems faced daily by those who use the technology and computer science, both ordinary users and technicians. But where did, why and what is the purpose of this site?

Click here to read!

Siga o Dicas em Geral no Google+

Video of the week