Diferència entre revisions de la pàgina «Solucio enviament dades php»
De wikiserver
(Es crea la pàgina amb «Solució: <source lang="php"> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http:…».) |
|||
(Hi ha 2 revisions intermèdies del mateix usuari que no es mostren) | |||
Línia 1: | Línia 1: | ||
− | Solució: | + | '''Solució 1:''' |
<source lang="php"> | <source lang="php"> | ||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> | ||
Línia 121: | Línia 121: | ||
} | } | ||
} | } | ||
+ | } | ||
+ | ?> | ||
+ | |||
+ | </body> | ||
+ | </html> | ||
+ | </source> | ||
+ | |||
+ | |||
+ | |||
+ | '''SOLUCIÓ 2:''' | ||
+ | <source lang="php"> | ||
+ | <!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"> | ||
+ | <head> | ||
+ | <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> | ||
+ | |||
+ | <title>Exemple formularis</title> | ||
+ | </head> | ||
+ | |||
+ | <body> | ||
+ | |||
+ | <?php | ||
+ | |||
+ | if (isset($_POST['enviar'])){ | ||
+ | |||
+ | foreach ($_POST as $key => $value) { | ||
+ | echo $key.": ".$value."<br>"; | ||
+ | } | ||
+ | } | ||
+ | else{ | ||
+ | ?> | ||
+ | |||
+ | |||
+ | |||
+ | <FORM action="index.php" method="POST" name="entrada"> | ||
+ | |||
+ | <table> | ||
+ | |||
+ | <tr> | ||
+ | <td align="right">Nom</td> | ||
+ | <td align="left"> <input type="text" name="nom" size="50" maxlength="20" /> </td> | ||
+ | </tr> | ||
+ | |||
+ | <tr> | ||
+ | <td align="right">Cognom</td> | ||
+ | <td align="left"> <input type="text" name="cognom" size="50" maxlength="20" /> </td> | ||
+ | </tr> | ||
+ | |||
+ | <tr> | ||
+ | <td align="right">Passwd</td> | ||
+ | <td align="left"> <input type="password" name="password" size="50" maxlength="20" /> </td> | ||
+ | </tr> | ||
+ | |||
+ | <tr> | ||
+ | <td align="right">Usuari</td> | ||
+ | <td align="left"> <INPUT type="radio" name="usuari" value="professor" checked> Profe | ||
+ | <INPUT type="radio" name="usuari" value="alumne"> Alumne | ||
+ | </td> | ||
+ | </tr> | ||
+ | |||
+ | <tr> | ||
+ | <td align="right">Actiu</td> | ||
+ | <td align="left"> <INPUT type="checkbox" name="actiu" checked> </td> | ||
+ | </tr> | ||
+ | |||
+ | <tr> | ||
+ | <td align="right">Foto</td> | ||
+ | <td align="left"> <INPUT type="file" name="foto" size="35"></td> | ||
+ | </tr> | ||
+ | |||
+ | <tr> | ||
+ | <td align="right">Edat</td> | ||
+ | <td align="left"> | ||
+ | |||
+ | <SELECT name="edat"> | ||
+ | <?php | ||
+ | //Creem un petit codi per no haver d'introduir tots els valors al HTML manualment | ||
+ | for($i=18; $i<=99; $i++) | ||
+ | { | ||
+ | echo '<option value="'.$i.'">'.$i; | ||
+ | } | ||
+ | ?> | ||
+ | </SELECT> | ||
+ | </td> | ||
+ | </tr> | ||
+ | |||
+ | <tr> | ||
+ | <td align="right" valign="top">Comentaris</td> | ||
+ | <td align="left"> | ||
+ | <TEXTAREA name="comentaris" rows="10" cols="38" >(Comentaris de l'usuari) </TEXTAREA> | ||
+ | </td> | ||
+ | </tr> | ||
+ | |||
+ | |||
+ | <tr> | ||
+ | <td> <INPUT type="hidden" name="ocult" value="prova"></td> | ||
+ | <td colspan="2" align="center"><INPUT type="submit" value="Enviar" name="enviar"> <INPUT type="reset" value="Esborrar" ></td> | ||
+ | |||
+ | </tr> | ||
+ | |||
+ | |||
+ | |||
+ | </table> | ||
+ | |||
+ | |||
+ | </FORM> | ||
+ | |||
+ | <?php | ||
} | } | ||
?> | ?> |
Revisió de 18:25, 3 oct 2018
Solució 1:
<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Exemple formularis</title>
</head>
<body>
<?php
//Primera vegada que entrem al formulari
//Si la variable nom està buida, és que el formulari no s'ha executat
if(!isset($_POST["nom"]))
{
//Per si de cas, mostrem per pantalla tot el contingut de POST.
//Si hi ha cap valor, ho veurem
if(isset($_POST))
{
foreach ($_POST as $index=>$value)
{
echo "POST[$index] = $value<br>";
}
}
?>
<FORM action="01formulari.php" method="POST" name="entrada">
<table>
<tr>
<td align="right">Nom</td>
<td align="left"> <input type="text" name="nom" size="50" maxlength="20" /> </td>
</tr>
<tr>
<td align="right">Cognom</td>
<td align="left"> <input type="text" name="cognom" size="50" maxlength="20" /> </td>
</tr>
<tr>
<td align="right">Passwd</td>
<td align="left"> <input type="password" name="password" size="50" maxlength="20" /> </td>
</tr>
<tr>
<td align="right">Usuari</td>
<td align="left"> <INPUT type="radio" name="usuari" value="professor" checked> Profe
<INPUT type="radio" name="usuari" value="alumne"> Alumne
</td>
</tr>
<tr>
<td align="right">Actiu</td>
<td align="left"> <INPUT type="checkbox" name="actiu" checked> </td>
</tr>
<tr>
<td align="right">Foto</td>
<td align="left"> <INPUT type="file" name="foto" size="35"></td>
</tr>
<tr>
<td align="right">Edat</td>
<td align="left">
<SELECT name="edat">
<?php
//Creem un petit codi per no haver d'introduir tots els valors al HTML manualment
for($i=18; $i<=99; $i++)
{
echo '<option value="'.$i.'">'.$i;
}
?>
</SELECT>
</td>
</tr>
<tr>
<td align="right" valign="top">Comentaris</td>
<td align="left">
<TEXTAREA name="comentaris" rows="10" cols="38" >(Comentaris de l'usuari) </TEXTAREA>
</td>
</tr>
<tr>
<td> <INPUT type="hidden" name="ocult" value="prova"></td>
<td colspan="2" align="center"><INPUT type="submit" value="Enviar"> <INPUT type="reset" value="Esborrar" ></td>
</tr>
</table>
</FORM>
<?php
}
else //Processem el contingut del formulari
{
//Mostrem el contingut sencer de POST
if(isset($_POST))
{
foreach ($_POST as $index=>$value)
{
echo "POST[$index] = $value<br>";
}
}
}
?>
</body>
</html>
SOLUCIÓ 2:
<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Exemple formularis</title>
</head>
<body>
<?php
if (isset($_POST['enviar'])){
foreach ($_POST as $key => $value) {
echo $key.": ".$value."<br>";
}
}
else{
?>
<FORM action="index.php" method="POST" name="entrada">
<table>
<tr>
<td align="right">Nom</td>
<td align="left"> <input type="text" name="nom" size="50" maxlength="20" /> </td>
</tr>
<tr>
<td align="right">Cognom</td>
<td align="left"> <input type="text" name="cognom" size="50" maxlength="20" /> </td>
</tr>
<tr>
<td align="right">Passwd</td>
<td align="left"> <input type="password" name="password" size="50" maxlength="20" /> </td>
</tr>
<tr>
<td align="right">Usuari</td>
<td align="left"> <INPUT type="radio" name="usuari" value="professor" checked> Profe
<INPUT type="radio" name="usuari" value="alumne"> Alumne
</td>
</tr>
<tr>
<td align="right">Actiu</td>
<td align="left"> <INPUT type="checkbox" name="actiu" checked> </td>
</tr>
<tr>
<td align="right">Foto</td>
<td align="left"> <INPUT type="file" name="foto" size="35"></td>
</tr>
<tr>
<td align="right">Edat</td>
<td align="left">
<SELECT name="edat">
<?php
//Creem un petit codi per no haver d'introduir tots els valors al HTML manualment
for($i=18; $i<=99; $i++)
{
echo '<option value="'.$i.'">'.$i;
}
?>
</SELECT>
</td>
</tr>
<tr>
<td align="right" valign="top">Comentaris</td>
<td align="left">
<TEXTAREA name="comentaris" rows="10" cols="38" >(Comentaris de l'usuari) </TEXTAREA>
</td>
</tr>
<tr>
<td> <INPUT type="hidden" name="ocult" value="prova"></td>
<td colspan="2" align="center"><INPUT type="submit" value="Enviar" name="enviar"> <INPUT type="reset" value="Esborrar" ></td>
</tr>
</table>
</FORM>
<?php
}
?>
</body>
</html>