Manual of PHP

 
 
 

Web map

 
rim   rim
Carriedarrow Forms with PHP

 

Forms with PHP To print E-mail

The Forms are not part of PHP, but of the standard language of Internet, HTML. We are going to dedicate in this chapter some lines to the HTML, them to begin later to treat with PHP.

Everything formulaic begins with the tag <FORM ACTION = "lo_que_sea.php" METHOD = "post/get">. With. With ACTION we indicate the script that goes to process the information that we gather in the form, while METHOD indicates us if the user of the form goes it is to send information (post) or to gather them (get). The tag <FORM> indicates the end of the form.

From the tag <FORM> there come the fields of entry of information that can be:

Text picture:

<unputt type = "text" yam = "name" size = "jose" values "20" =>

Text picture with displacement bars:

<textarea rows = "5" yam = "description" cols = "20"> Is of red color </textarea>

Cross-check pigeonhole:

<unputt type = "checkbox" yam = to "change" values = "ON">

Option button:

<unputt type = "radio" values = "blue" checked yam = "color">

Menu desplegable:

<select size = "1" yam = "day">

<option selected I valued = "Monday"> on Monday </option>

<option> on Tuesday </option>

<option I valued = "Wednesday"> on Wednesday </option>

</select>

Command button:

<unputt type = "submit" values = to "send" yam = to "send">

Secret field:

<unputt type = "hidden" yam = "age" values = "55">

The latter type of field turns out to be especially useful when that oars to spend secret information in a form.

Since habrás observed all the types of field have a modifying called yam, which is not different that the name of the variable with which we will gather the information in the script indicated by the modifier ACTION of the tag FORM FORM, with value we establish a value for defect. 

Next let's see an example, for which we will create a form in HTML like the one that continues and will call it formulario.htm:

<HTML>

<BODY>

<FORM METHOD = "post" ACTION = "mis_datos.php">

<unputt type = "hidden" yam = "age" values = "55">

<p> Your name <unputt type = "text" yam = "name" size = "jose" values "30" => </p>

<p> Your favorite system 

<select size = "1" yam = "system">

<option selected Linux values = "Linux"> </option>

<option Unix values = "Unix"> </option>

<option Macintosh values = "Macintosh"> </option>

<option value=&qmargin-left: 75 "> <option "Windows" values => Windows </option>

</select> </p>

<p> Do you like the soccer? <unputt type = "checkbox" yam = "soccer" values = "ON"> </p>

<p> Which is your sex? </p>

<blockquote>

<p> Man <unputt type = "radio" value = "man" checked yam = "sex"> </p>

<p> Woman <unputt type = "radio" yam = "woman" values "sex" => </p>

</blockquote>

<p> Love </p>

<p> <textarea rows = "5" yam = "love" cols = "28"> </textarea> </p>

<p> <unputt type = "submit" values = "to Send information" yam = to "send"> 

<unputt type = "animal - left: 50"> <unputt type = "reset" values = to "Restore" yam = "B2"> </p>

</FORM>

</BODY>

<HTML>

And now we believe the script PHP called from him formulaic mis_datos.php:

All the information is in the variable $_POST, since the form is sent by the method post.

<? PHP;

if (isset ($_POST [to 'send']) {

I begin "Hello <b>". $_POST ['name]." </b> how are you <BR> n";

I begin You "are". $_POST ['sex']." <BR> n";

I begin You "have". $_POST ['age']." <BR> n";

I begin "Your favorite system is". $_POST ['system']." <BR> n";

if (isset ($_POST ['soccer']) {

I begin "You like the soccer <BR> n";

} else odigo" style = "margin-left: 50">} else {

I begin "you do not like the soccer <BR> n";

}

if ($_POST ['love]! =" ") {

I begin "Your love is: <BR> n";

I begin nl2br ($_POST ['love]);

} else {

I begin "you do not have love <BR> n";

}

}

I throw "<to href ='formulario.htm'> TO RETURN TO THE FORM </to>"

?>

As soon as the information of the form was refilled, we touch the button to Send information, with what I him stand out to send capture what his tag values indicates, that is to say to send = "to Send information". In our script the first thing that we evaluate is that the form has been sent, and for it nothing better than to verify that the variable $enviar is not empty. We set the sign dollar to send ahead, set the sign dollar to send ahead, since in PHP all the variables one recounts them with this sign.

It is necessary to bear in mind that if we were fusing the code of both files, we would save ourselves one, but not also it is possible to do in two as we are doing it. If the variable $enviar is empty, we send the form.

<? PHP;

if ($enviar) {

I begin "Hello <b>". $nombre. "</b> how are you <BR> n";

I begin You "are". $sexo. "<BR> n";

I begin You "have". $edad. "<BR> n";

I begin "Your favorite system is". $sistema. "<BR> n";

if ($futbol) {

I begin "You like the soccer <BR> n";

} else {

I begin "you do not like the soccer <BR> n";

}

if ($aficiones! = "") {

<stuot;)>

I begin "Your love is: <BR> n";

I begin nl2br ($aficiones);

} else {

I begin "you do not have love <BR> n";

}

I begin "<to href = '$PHP_SELF'> TO RETURN TO THE FORM </to>"

} else {

<HTML>

<BODY>

<FORM METHOD = "post" ACTION = "<? Did PHP begin $PHP_SELF?>">

<unputt type = "hidden" yam = "age" values = "55">

<p> Your name <unputt type = "text" yam = "name" size = "30" name" size = "jose" value "30" => </p>

<p> Your favorite system 

<select size = "1" yam = "system">

<option selected Linux values = "Linux"> </option>

<option Unix values = "Unix"> </option>

<option Macintosh values = "Macintosh"> </option>

<option "Windows" values => Windows </option>

</select> </p>

<p> Do you like the soccer? <unputt type = "checkbox" yam = "soccer" values = "ON"> </p>

<p> Which is your sex? </p>

<blockquote>

<p> Man <unputt type = "radio" value = "man" checked yam = "sex"> </p>

<p> = "code" style = "margin-left: 100"> <p> Woman <unputt type = "radio" yam = "woman" values "sex" => </p>

</blockquote>

<p> Love </p>

<p> <textarea rows = "5" yam = "love" cols = "28"> </textarea> </p>

<p> <unputt type = "submit" values = "to Send information" yam = to "send"> 

<unputt type = "reset" values = to "Restore" yam = "B2"> </p>

</FORM>

</BODY>

</HTML>

<? PHP

} //end IF

?>

The environment variable $PHP_SELF, it is a variable of environment that returns us the name of the script that we are executing. And finally, to make to notice the use of the function nl2br (), nl2br (), with which we replace the comebacks of car of the text, which the navigators do not recognize, with the tag <BR>.

It discharges of files from a form

We are going to see a special case, like unloading a file from a form. For it we will use a tag INPUT of type FILE, supported from the versions of the navigators Nestcape Navigator 2.0 and Internet Explorer 4.0.

The form must use the method post, and the attribute post, and the attribute enctype must have the value multipart/form-data. Also we must add to the form a secret field of name MAX_FILE_SIZE, to which we will give him the value in bytes of the maximum size of the file to be unloaded.

<FORM ENCTYPE = "multipart/form-data" ACTION = "7-3.php" METHOD = "post">

<INPUT TYPE = "hidden" yam = "MAX_FILE_SIZE" values = "100000"> 

<INPUT YAM = "file" TYPE = "file"

<INPUT TYPE = "submit" VALUES = "to Unload File"> 

</FORM>

When the form is sent, PHP will detect automatically that is unloading a file and will place it in a temporary directory in the servant. The above mentioned directory will be that the one that is indicated in the configuration file php.ini, or in his defect in the temporary directory of the system. 

When PHP detects that a file is unburdening itself it creates several variables with the prefix of the name of the file but with different completions. The variable finished in $_FILES ['file'] ['yam] contains the original name of the file, $_FILES ['file'] ['size'] contains the size in bytes of this one, and the variable $_FILES ['file'] ['type'] will indicate us the type of file if this one is offered by the navigator.

If the discharge process has not been correct variable file will take the value none and _size will be 0, and if the process has been correct, but the variable $_FILES ['file'] ['size'] gives 0, it means that the file overcomes discharge the maximum size indicated for MAX_FILE_SIZE.

As soon as there was unloaded the file, the first thing that we must do it is to move it to another place, since but nothing is done with him, when it finishes the execution of the page it will resign.

Let's see an example of everything above mentioned.

<HTML>

<BODY>

<? PHP

if (isset ($_POST [to 'send']) {

if ($_FILES ['file] ['yam']! =" "&& $_FILES [I 'file'] ['size']! = 0) {

I begin "Name: $archivo_name <BR> n";

I begin "Size: $archivo_size <BR> n";

I begin "Type: $archivo_type <BR> n";

if (! move_uploaded_file ($_FILES ['file] ['tmp_name'], "directory /". $_FILES [I 'file'] ['yam])) {

I begin "<h2> it was not possible to have copied the file </h2> n";

}

} elseif ($_FILES ['file] ['yam']! =" "&& $FILES [I 'file'] ['size'] == 0) {

I begin "<h2> Size of overcome file </h2> n";

} else {

I begin "<h2> he has not chosen a file to discharge </h2> n";

I begin "<HR> n";

}

?>

<FORM ENCTYPE = "multipart/form-data" ACTION = "<? php I begin $_SERVER ['PHP_SELF'];?>" METHOD = "post"

<INPUT type = "hidden" yam = "MAX_FILE_SIZE" values = "100000"> 

<p> <b> File to be unloaded <b> <br> 

<INPUT type = "file" yam = I "file" size = "35"> </p> 

<p> <INPUT type = "submit" yam = to "send" values = to "Accept"> </p>

</FORM>

</BODY>

</HTML>

 

Previous chapter:
Meetings

 

 
Front
Chapters of the Manual of PHP
Introduction to PHP
Variables
My First Script PHP
Operators in PHP
Structures of Control
Functions in PHP
Code inclusion
Counterfoils (array)
Chains of Characters
Classes
Dates
Entry and Exit
Operations with Files
The language SQL and PHP
Connection with MySQL
Meetings
Forms with PHP
To practise in line
List of practices in line
FAQ
Frequent questions
Codes PHP
List of Codes PHP
Forums
Forums PHP
Other Manuals
Manuals of other languages
 
   
 
 
MySpaceScripts.info - Sitemap - Contact us - Sitemap - Contact us - Sitemap - Contact us

incoming tour operator poland , verizon wireless prepaid , Fantasy book reviews , motorization.famous-car.co.uk , SEO , waagen , Li-Ion Battery for IBM