Diferencia entre revisiones de «PHP Paso de variables entre páxinas»
Ir a la navegación
Ir a la búsqueda
| Línea 7: | Línea 7: | ||
: O formato que teñen este tipo de parámetros é: param1=valor¶m2=valor&..... | : O formato que teñen este tipo de parámetros é: param1=valor¶m2=valor&..... | ||
| − | * | + | * '''Desvantaxes:''' |
:* Calquera persoa pode ver os valores das variables polo que dito método non é moi seguro. | :* Calquera persoa pode ver os valores das variables polo que dito método non é moi seguro. | ||
:* O usuario pode cambiar o valor de ditas variables polo que pode suceder que o sitio amose ou faga algo diferente do permitido. | :* O usuario pode cambiar o valor de ditas variables polo que pode suceder que o sitio amose ou faga algo diferente do permitido. | ||
:* O usuario pode obter información non actual se usa unha URL con datos non actualizados. | :* O usuario pode obter información non actual se usa unha URL con datos non actualizados. | ||
| + | :* O tamaño do que podemos enviar na URL está limitado (depende de cada navegador, pero pode estar entre 2KB-8KB) | ||
| + | |||
| + | * '''Vantaxes:''' | ||
| + | |||
| + | :* A través da URL podemos pasar datos dunha páxina a outra sen necesidade de ter un formulario por medio (o veremos no exemplo seguinte). | ||
| + | |||
| Línea 18: | Línea 24: | ||
: Vexamos un exemplo de código: | : Vexamos un exemplo de código: | ||
| − | '''Arquivo: | + | '''Arquivo: UD2_Ex1_eleccion_pelicula.php''' |
::<syntaxhighlight lang="php" line enclose="div" highlight="11" > | ::<syntaxhighlight lang="php" line enclose="div" highlight="11" > | ||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" | ||
| Línea 30: | Línea 36: | ||
<body> | <body> | ||
<?php | <?php | ||
| − | printf("Mandamos un nome de película fixo na url:<a href=' | + | printf("Mandamos un nome de película fixo na url:<a href='UD2_Ex1_datos_pelicula.php?pelicula=SUPERMAN'>PELICULA</a>"); |
?> | ?> | ||
</body> | </body> | ||
| Línea 37: | Línea 43: | ||
| − | '''Arquivo: | + | '''Arquivo: UD2_Ex1_datos_pelicula.php''' |
::<syntaxhighlight lang="php" line enclose="div" highlight="12" > | ::<syntaxhighlight lang="php" line enclose="div" highlight="12" > | ||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" | ||
| Línea 70: | Línea 76: | ||
: Vexamos unha modificación do código anterior, pasando un nome de película con espazos: | : Vexamos unha modificación do código anterior, pasando un nome de película con espazos: | ||
| − | '''Arquivo: | + | '''Arquivo: UD2_Ex1_eleccion_pelicula.php''' |
::<syntaxhighlight lang="php" line enclose="div" highlight="11" > | ::<syntaxhighlight lang="php" line enclose="div" highlight="11" > | ||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" | ||
| Línea 84: | Línea 90: | ||
$pelicula=urlencode("E.T. El extraterrestre"); | $pelicula=urlencode("E.T. El extraterrestre"); | ||
echo "Mandamos un nome de película fixo na url:" | echo "Mandamos un nome de película fixo na url:" | ||
| − | . "<a href=' | + | . "<a href='UD2_Ex1_datos_pelicula.php?pelicula=$pelicula'>PELICULA</a>"; |
?> | ?> | ||
</body> | </body> | ||
| Línea 91: | Línea 97: | ||
| − | '''Arquivo: | + | '''Arquivo: UD2_Ex1_datos_pelicula.php''' |
::<syntaxhighlight lang="php" line enclose="div" highlight="12" > | ::<syntaxhighlight lang="php" line enclose="div" highlight="12" > | ||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" | ||
| Línea 115: | Línea 121: | ||
| − | * Vexamos agora un exemplo de decodificación dunha cadea: | + | * Vexamos agora un exemplo de código de decodificación dunha cadea: |
::<syntaxhighlight lang="php" line enclose="div" highlight="12" > | ::<syntaxhighlight lang="php" line enclose="div" highlight="12" > | ||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" | ||
| Línea 140: | Línea 146: | ||
| + | * Vexamos agora un exemplo <u>utilizando un formulario</u>: | ||
| + | |||
| + | '''Arquivo: UD2_Ex2_eleccion_pelicula_formulario''' | ||
| + | ::<syntaxhighlight lang="php" line enclose="div" highlight="27" > | ||
| + | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" | ||
| + | "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> | ||
| + | <html xmlns="http://www.w3.org/1999/xhtml" lang="es" xml:lang="es"> | ||
| + | <head> | ||
| + | <meta charset="utf-8" /> | ||
| + | <title>Formulario</title> | ||
| + | <style> | ||
| + | .formularios{ | ||
| + | width: 30%; | ||
| + | margin: 0px auto; | ||
| + | } | ||
| + | .etiqueta{ | ||
| + | float: left; | ||
| + | width: 190px; | ||
| + | } | ||
| + | .botonesformularios{ | ||
| + | width: 50%; | ||
| + | margin: 30px auto; | ||
| + | text-align: center; | ||
| + | } | ||
| + | select { | ||
| + | width:50%; | ||
| + | } | ||
| + | </style> | ||
| + | </head> | ||
| + | <body> | ||
| + | <form id="frmPelicula" class="formularios" method="get" action="UD2_Ex2_datos_pelicula_formulario.php"> | ||
| + | <div> | ||
| + | <div class='etiqueta'>Película preferida:</div> | ||
| + | <select name="lstPelicula"><option>E.T. El extraterrestre</option><option>Supermán</option></select> | ||
| + | </div> | ||
| + | <div> | ||
| + | <div class='etiqueta'>Xénero preferido:</div> | ||
| + | <select name="lstXenero"><option>Acción</option><option>Ciencia ficción</option></select> | ||
| + | </div> | ||
| + | <div class="botonesformularios"> | ||
| + | <input type='submit' value="ENVIAR" /> | ||
| + | </div> | ||
| + | </form> | ||
| + | |||
| + | </body> | ||
| + | </html> | ||
| + | </php> | ||
| + | </syntaxhighlight> | ||
| + | |||
| + | :* Fixarse que enviamos os datos utilizando o '''método get'''. | ||
| + | |||
| + | '''Arquivo: UD2_Ex2_datos_pelicula_formulario''' | ||
| + | ::<syntaxhighlight lang="php" line enclose="div" highlight="27" > | ||
| + | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" | ||
| + | "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> | ||
| + | |||
| + | <html> | ||
| + | <head> | ||
| + | <meta charset="UTF-8"> | ||
| + | <title></title> | ||
| + | </head> | ||
| + | <body> | ||
| + | <?php | ||
| + | echo 'Os datos que escolliches son:<br/><br/>'; | ||
| + | |||
| + | $pelicula = $_GET['lstPelicula']; | ||
| + | $xenero = $_GET['lstXenero']; | ||
| + | |||
| + | printf("PELICULA:%s<br/>XÉNERO:%s",$pelicula,$xenero); | ||
| + | ?> | ||
| + | |||
| + | </body> | ||
| + | </html> | ||
| + | </syntaxhighlight> | ||
| + | |||
| + | == Utilizando o método POST == | ||
<br> -- [[Usuario:angelfg|Ángel D. Fernández González]] -- (2016). | <br> -- [[Usuario:angelfg|Ángel D. Fernández González]] -- (2016). | ||
Revisión del 09:17 20 sep 2016
A través da URL
- Unha forma de pasar información de unha páxina a outra é a través dunha URL.
- Se vos fixades cando facedes unha busca en google (por exemplo buscade 'php') na url aparecerá algo coma isto: https://www.google.es/search?q=php&ie=utf-8&oe=utf-8&client=firefox-b&gfe_rd=cr&ei=a1XgV4CmOu6s8wfuibOQAQ
- Os datos que aparecen despois do signo '?' son parámetros:?q=php&ie=utf-8&oe=utf-8
- O formato que teñen este tipo de parámetros é: param1=valor¶m2=valor&.....
- Desvantaxes:
- Calquera persoa pode ver os valores das variables polo que dito método non é moi seguro.
- O usuario pode cambiar o valor de ditas variables polo que pode suceder que o sitio amose ou faga algo diferente do permitido.
- O usuario pode obter información non actual se usa unha URL con datos non actualizados.
- O tamaño do que podemos enviar na URL está limitado (depende de cada navegador, pero pode estar entre 2KB-8KB)
- Vantaxes:
- A través da URL podemos pasar datos dunha páxina a outra sen necesidade de ter un formulario por medio (o veremos no exemplo seguinte).
- As variables que se pasan desta forma poden ser referenciadas en PHP a través da matriz global $_GET['param'].
- Vexamos un exemplo de código:
Arquivo: UD2_Ex1_eleccion_pelicula.php
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 2 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 3 4 <html> 5 <head> 6 <meta charset="UTF-8"> 7 <title></title> 8 </head> 9 <body> 10 <?php 11 printf("Mandamos un nome de película fixo na url:<a href='UD2_Ex1_datos_pelicula.php?pelicula=SUPERMAN'>PELICULA</a>"); 12 ?> 13 </body> 14 </html>
Arquivo: UD2_Ex1_datos_pelicula.php
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 2 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 3 <html xmlns="http://www.w3.org/1999/xhtml" lang="es" xml:lang="es"> 4 <head> 5 <meta charset="utf-8" /> 6 <title>Exemplos de códigos php</title> 7 </head> 8 <body> 9 10 <?php 11 12 printf("A película pasada como parámetro na URL é: <b>%s</b>",$_GET['pelicula']); 13 14 15 ?> 16 </body> 17 </html>
- Podedes comprobar como se modificades a url e poñedes outra valor, premendo a tecla 'enter' para que volva a cargar a páxina aparece o novo valor posto.
Pasando caracteres especiais na URL
- O problema que podemos ter pasando valores desta forma, é que ditos valores teñan espazos en branco ou caracteres especiais.
- Para poder pasar estes valores podemos facer uso da urlencode.
- Para facer a función inversa: urldecode. Pero isto non é necesario no caso de asinar o valor a unha variable na segunda páxina.
- Vexamos unha modificación do código anterior, pasando un nome de película con espazos:
Arquivo: UD2_Ex1_eleccion_pelicula.php
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 2 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 3 4 <html> 5 <head> 6 <meta charset="UTF-8"> 7 <title></title> 8 </head> 9 <body> 10 <?php 11 $pelicula=urlencode("E.T. El extraterrestre"); 12 echo "Mandamos un nome de película fixo na url:" 13 . "<a href='UD2_Ex1_datos_pelicula.php?pelicula=$pelicula'>PELICULA</a>"; 14 ?> 15 </body> 16 </html>
Arquivo: UD2_Ex1_datos_pelicula.php
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 2 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 3 <html xmlns="http://www.w3.org/1999/xhtml" lang="es" xml:lang="es"> 4 <head> 5 <meta charset="utf-8" /> 6 <title>Exemplos de códigos php</title> 7 </head> 8 <body> 9 10 <?php 11 12 $pelicula=$_GET['pelicula']; 13 echo "A película pasada como parámetro na URL é: <b>$pelicula</b>"; 14 15 ?> 16 </body> 17 </html>
- Vexamos agora un exemplo de código de decodificación dunha cadea:
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 2 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 3 <html xmlns="http://www.w3.org/1999/xhtml" lang="es" xml:lang="es"> 4 <head> 5 <meta charset="utf-8" /> 6 <title>Exemplos de códigos php</title> 7 </head> 8 <body> 9 10 <?php 11 12 $cadeaCodificada="<br/>Espero + que + esto + se lea + ben"; 13 echo $cadeaCodificada; 14 echo urldecode($cadeaCodificada); 15 16 ?> 17 </body> 18 </html>
- Vexamos agora un exemplo utilizando un formulario:
Arquivo: UD2_Ex2_eleccion_pelicula_formulario
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 2 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 3 <html xmlns="http://www.w3.org/1999/xhtml" lang="es" xml:lang="es"> 4 <head> 5 <meta charset="utf-8" /> 6 <title>Formulario</title> 7 <style> 8 .formularios{ 9 width: 30%; 10 margin: 0px auto; 11 } 12 .etiqueta{ 13 float: left; 14 width: 190px; 15 } 16 .botonesformularios{ 17 width: 50%; 18 margin: 30px auto; 19 text-align: center; 20 } 21 select { 22 width:50%; 23 } 24 </style> 25 </head> 26 <body> 27 <form id="frmPelicula" class="formularios" method="get" action="UD2_Ex2_datos_pelicula_formulario.php"> 28 <div> 29 <div class='etiqueta'>Película preferida:</div> 30 <select name="lstPelicula"><option>E.T. El extraterrestre</option><option>Supermán</option></select> 31 </div> 32 <div> 33 <div class='etiqueta'>Xénero preferido:</div> 34 <select name="lstXenero"><option>Acción</option><option>Ciencia ficción</option></select> 35 </div> 36 <div class="botonesformularios"> 37 <input type='submit' value="ENVIAR" /> 38 </div> 39 </form> 40 41 </body> 42 </html> 43 </php>
- Fixarse que enviamos os datos utilizando o método get.
Arquivo: UD2_Ex2_datos_pelicula_formulario
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 2 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 3 4 <html> 5 <head> 6 <meta charset="UTF-8"> 7 <title></title> 8 </head> 9 <body> 10 <?php 11 echo 'Os datos que escolliches son:<br/><br/>'; 12 13 $pelicula = $_GET['lstPelicula']; 14 $xenero = $_GET['lstXenero']; 15 16 printf("PELICULA:%s<br/>XÉNERO:%s",$pelicula,$xenero); 17 ?> 18 19 </body> 20 </html>
Utilizando o método POST
-- Ángel D. Fernández González -- (2016).