Let's take a look at the basic forms HTML and see the interaction with PHP.
A basic form consists of HTML \u0026lt;FORM> a set of parameters (which we'll cover the basics) and the set of fields that are submitted with the form. The most basic would be to define a form, eg
\u0026lt;FORMNAME='mi_formulario' ACTION='procesar.php' METHOD='POST'>
........
\u0026lt;/ FORM>
The 3 parameters that I included are:
• NAME: the name of the form then be referenced in the code (used especially the name we give when we work with javascript). It is highly recommended to use in conjunction with the parameter ID (ID = 'mi_formulario' following the example) and that depending on which browser a parameter may take precedence over another.
• ACTION: shows the landing page to which you should send the form data. As shown in the example, the data would be sent to the page procesar.php
• METHOD: Show you how to pass parameters:
• POST variables are sent as "hidden" (enough to know this for what we get in this workshop).
• GET: the variables are sent within the destination URL (visible).
For example if we pass a variable that is the ID of the person and the form we would use METHOD = GET in the browser bar a URL similar to:
http://miservidor/pagina.php?dni = 112335965
However, we would only http://miservidor/pagina.php POST and variable access to the DNI would "embedded" in the form.
this perspective, it seems that we should always use POST instead of GET, right? The user does not have to see manage those variables. This will be true most of the cases, but there are times when we have no choice but to use GET. Experience will tell you when you use either method.
A fourth parameter that we use a lot: TARGET. This setting helps us determine where to send the form data. ACTION determines the page that we must show that you must process the form fields, TARGET tell you where to display this information. You can take the values \u200b\u200b
• _self: this same frame or window.
• _blank: in a new window.
• _top: under the window on which all we are looking at (if we do not use frames in our page (frameset) equivalent to _self).
• _parent: The parent frame of the window we're looking at (if we use frameset with only one level of nesting, equivalent to _top).
• "element name" can indicate that the destination is an element of our capable of receiving information from a form. For example, a framework that we created and have a defined name, an IFRAME, etc ... generally refers to this "item name" as the name of a frame.
Within the form fields will be declared with the INPUT tag. Not going to review the different types of fields that exist but we will see how to deal directly in PHP.
Here's how to handle fields in a form that is received by a PHP page. Is the form that will be included on page c4-ejemplo1.html:
\u0026lt;FORM NAME="datos" ID="datos" ACTION="c4-e1.php" method="post">
Your name : \u0026lt;INPUT name="name" id="name" size="20" maxlength="20">
\u0026lt;BR>
Your email: \u0026lt;INPUT NAME="email" ID="email" size="20" maxlength="20">
\u0026lt;BR>
\u0026lt;INPUT TYPE = "SUBMIT" VALUE = "Submit ">
\u0026lt;/ FORM>
Enter your name and email and click the submit button. How do I check the value of these fields in the target PHP? PHP associative arrays built for the variables that we refer to a form:
• If we use METHOD = GET will have an associative array $ _GET ["varname"] each variable to reference the form.
• If we use METHOD = POST array will be $ _POST ["varname"]. Depending
well as server configuration be possible to use $ varname directory (eg $ dni or $ email). By way of commentary, this will depend on whether the variable register_globals is ON on the server. However, $ _POST and $ _GET are always available, so the purpose of application portability is more advisable to use these arrays.
Therefore, if we construct the following PHP, we will display the information submitted by the form:
\u0026lt;HTML>
\u0026lt;HEAD>
\u0026lt;TITLE> Cap 4 Example 1 \u0026lt;/ TITLE>
\u0026lt;/ HEAD>
\u0026lt;BODY>
Your ID is \u0026lt;? echo $ _POST ["ID"];?>
Your email is \u0026lt;? echo $ _POST ["email"];?>
\u0026lt;/ BODY>
\u0026lt;/ HTML>
See the complete example here: http://phpdesdecero.host.sk/c4-ejemplo1 . html
All in one Philosophy
Too often we agree (or need) to keep the screen the same form that we sent to PHP. To achieve this, build the form within a PHP page and access the variables as follows:
\u0026lt;HTML>
\u0026lt;HEAD>
\u0026lt;TITLE> Cap 4 Example 2 \u0026lt;/ TITLE>
\u0026lt; ; / HEAD>
\u0026lt;BODY>
\u0026lt;FORM NAME="datos" ID="datos" ACTION= "c4-e2.php"
method="post">
Tu nombre: <INPUT NAME="nombre" ID="nombre" size="20" maxlength="20" VALUE="<? echo $_POST["nombre"]; ?>" >
<BR>
Tu email: <INPUT NAME="email" ID="email" size="20" maxlength="20" VALUE="<? echo $_POST["email"]; ?>" >
<BR>
<INPUT TYPE="SUBMIT" VALUE="Enviar">
</FORM>
<?
if (strlen ($ _POST ['name'])> 0) {
echo "Your name is \u0026lt;BR>"
echo $ _POST [ "name"];
}
if (strlen ($ _POST ["email"])> 0) {
echo "Your email is \u0026lt;BR>"
echo $ _POST ["email"];
}
?>
\u0026lt;/ BODY>
\u0026lt;/ HEAD>
I highlighted in blue that we have changed or Added:
• If we let the form's ACTION is the very page you're viewing, the values \u200b\u200bthat are introduced after viewing the first page will be sent to our web site.
• By putting each INPUT VALUE property we are assigning a default value. If you indicate that the default is "PHP variable containing the" we "conserve" submitted on the form.
• In the final section I have included the use of PHP strlen (equivalent to the same function in C). This function returns the number of characters that comprise the variable that is passed by argument. What we have done has been to say "if the variable contains more than zero characters, run these sentences." The sentences for their part, are very simple: to show a fixed text message and then the value of the variable.
can see the example running on http://phpdesdecero.host.sk/c4-e2.php
0 comments:
Post a Comment