feb 10

Para extrar datos de un formulario html con PHP, debemos tener en cuenta dos parametros, que son los siguientes:

  1. El nombre que le hemos dado a cada campo de formulario html
  2. El archivo a la que se envían los datos del formulario.

Teniendo en cuenta estos datos, vamos a hacer una prueba con una supuesta librería on-line, con este fichero html llamado “pedido.htm”:


<html>
<body>
<table border="1" width="100%">
<tr>
<td colspan="2" width="100%"></td>
</tr>
<tr>
<td width="50%">El Símbolo Perdido</td>
<td width="50%">
<form action="compra.php" method="post">
<input name="el_simbolo_perdido" size="20" type="text" /></form></td>
<tr>
<td width="50%">La Catedral del Mar</td>
<td width="50%"><input name="la_catedral_del_mar" size="20" type="text" /></td>
</tr>
<tr>
<td width="50%">Crepúsculo</td>
<td width="50%"><input name="crepusculo" size="20" type="text" /></td>
</tr>
<tr>
<td width="50%">Harry Potter</td>
<td width="50%"><input name="harry_potter" size="20" type="text" /></td>
</tr>
<tr>
<td colspan="2" width="100%">
<input name="enviar" type="submit" value="Enviar" /></td>
</tr>
</tr>
</table>
</body>
</html>

En color verde, podemos ver el inicio del formulario en el que se indica al archivo al que va destinado (compra.php).

En color rojo, podemos ver los diferentes nombres libros (campos de formularios) a poder comprar.

El resultado delformulario sería este (los links están desctivados):

El Símbolo Perdido
La Catedral del Mar
Crepúsculo
Harry Potter

Para poder extraer los datos del formulario a una página PHP tenemos que unilizar la función $_POST. Esta función es así:

$nombre_variable = $_POST["nombre_campo_formulario"];

Los campos marcados de rojo pueden varían:

  1. El nombre de la variable es el que tu quieras, (pero es recomendable que sea el mismo que el del campo de formulario)
  2. El nombre del campo de formulario a de ser el mismo que el del campo “name” en el html.

Sabiendo estos parámetros vamos a crear el archivo “compra.php”:


<html>
<?php
$el_simbolo_perdido = $_POST["el_simbolo_perdido"];
$la_catedral_del_mar = $_POST["la_catedral_del_mar"];
$crepusculo = $_POST["crepusculo"];
$harry_potter = $_POST["harry_potter"];
?>
</html>

En este paso hemos transformado los datos del formulario (rojo) en variables de PHP(azul). Ahora tenemos que indicar al público el número de libros comprados. Para ello, creamos una tabla en html en la que insertamos los datos php:


<html>

<?php
  $el_simbolo_perdido = $_POST["el_simbolo_perdido"];
  $la_catedral_del_mar = $_POST["la_catedral_del_mar"];
  $crepusculo = $_POST["crepusculo"];
 <$harry_potter = $_POST["harry_potter"];
?>

<body>
  <table border="1" width="100%">
    <tr>
      <td>LIBRO</td>
      <td>NÚMERO DE LIBROS</td>
    </tr>
    <tr>
      <td>El símbolo perdido</td>
      <td><?php echo "$el_simbolo_perdido";?></td>
    </tr>
    <tr>
      <td>La catedral del mar</td>
      <td><?php echo "$la_catedral_del_mar";?></td>
    </tr>
    <tr>
      <td>Crepúsculo</td>
      <td><?php echo "$crepusculo";?></td>
    </tr>
    <tr>
      <td>Harry Potter</td>
      <td><?php echo $harry_potter;?></td>
    </tr>
      <td>TOTAL DE LIBROS</td>
      <td><?php echo $el_simbolo_perdido + $la_catedral_del_mar + $crepusculo + $harry_potter; ?></td>
    </tr>
  </table>
</body>
</html>

Y ya hemos terminado el archivo para extraer datos de un formulario html con php.

¿Quieres probar la practica que hemos hecho? Pulsa aquí


6 comentarios

  • Horacio Said on abril 22nd, 2011 at 4:55 pm:

    Muchas gracias.. estoy aprendiendo php y en un video tutorial que estoy siguiente le falta declarar las variables por eso no me levantaba el valor de las misma.. agradecido!!

  • Excerpt Said on septiembre 25th, 2011 at 12:16 pm:

    Excerpt…

    [...]and we’d reasonably do our submitting than wade via it. Lately, every thing in marketing is so far outdoors the field, we don’t even know what the[...]…

  • Excerpt Said on septiembre 30th, 2011 at 3:42 am:

    Excerpt…

    [...]website must have good colours, visible effects, and a catchy net address with the intention to be successful. Although these items are essential, they[...]…

  • Excerpt Said on octubre 10th, 2011 at 4:36 am:

    Excerpt…

    [...]Look for a program that is of high quality. For instance, look for one that is associated with many [...]…

  • Jerseys Said on noviembre 22nd, 2011 at 5:10 pm:

    where to buy Premier and Stitched Colorado Rockies jerseys ??…

    [...]we adivce go to this website to see more about Colorado Rockies jerseys and others. [...]…

  • Spencer Machacek Jerseys Said on diciembre 23rd, 2011 at 9:18 pm:

    Blogs ou should be reading…

    [...]Here is a Great Blog You Might Find Interesting that we Encourage You[...]?…

Responder.

*