GET e metodo POST$_GET, $_POST, $_REQUEST? e da coppie NomeParametro=ValoreParametro=&http://localhost/form_1/GET_maggiore.php?num1=2&num2=12
\_______________/ \___________/
script PHP parametri
| Parametro | Valore |
|---|---|
num1 |
2 |
num2 |
12 |
Nello script i valori si leggono dall'array associativo $_GET.
<html>
<!-- GET_maggiore1.html -->
<body>
<a href="GET_maggiore.php?num1=2&num2=12">
esegui GET_maggiore.php
</a>
</body>
</html>I parametri sono scritti a mano nell'URL del collegamento.
<?php
// GET_maggiore.php
// restituisce il valore maggiore dei due numeri
function maggiore($n1, $n2)
{
if ($n1 > $n2)
return $n1;
else
return $n2;
}
$n1 = $_GET['num1'];
$n2 = $_GET['num2'];
$max = maggiore($n1, $n2);
print("Il maggiore tra i due numeri $n1 e $n2 e': $max");
?>Il maggiore tra i due numeri 2 e 12 e': 12
I due numeri vengono ora inseriti dall'utente; lo script richiamato è
sempre GET_maggiore.php.
<html>
<!-- GET_maggiore2.html -->
<body>
<form action="GET_maggiore.php" method="get">
<input type="text" name="num1" /> inserisci il primo numero <br>
<input type="text" name="num2" /> inserisci il secondo numero <br><br>
<input type="submit" value="esegui GET_maggiore.php" /> <br>
</form>
</body>
</html>| Attributo | Elemento | Significato |
|---|---|---|
action |
<form> |
URL del server e nome del programma PHP da eseguire |
method |
<form> |
metodo di passaggio dei parametri: GET
o POST |
name |
<input> |
nome del parametro (es. num1, num2) |
Il valore di name diventa la
chiave dell'array $_GET
(o $_POST).
<html>
<!-- POST_maggiore.html -->
<body>
<form action="POST_maggiore.php" method="POST">
<input type="text" name="num1" /> inserisci il primo numero <br>
<input type="text" name="num2" /> inserisci il secondo numero <br><br>
<input type="submit" value="calcola il maggiore" /> <br>
</form>
</body>
</html>Cambia solo il valore dell'attributo method.
<?php
// POST_maggiore.php
// restituisce il valore maggiore dei due numeri
function maggiore($n1, $n2)
{
if ($n1 > $n2)
return $n1;
else
return $n2;
}
$n1 = $_POST['num1'];
$n2 = $_POST['num2'];
$m = maggiore($n1, $n2);
print("il maggiore tra i numeri $n1 e $n2 e' : $m");
?>I valori si leggono dall'array $_POST
invece che da $_GET.
http://localhost/form_1/POST_maggiore.php
| Aspetto | GET | POST |
|---|---|---|
| Dove viaggiano i dati | nell'URL (query string) | nel corpo della richiesta |
| Visibili nell'indirizzo | sì | no |
| Salvati nella cronologia | sì | no |
| Array PHP | $_GET |
$_POST |
| Uso tipico | ricerche, link condivisibili | login, dati sensibili |
$_REQUEST$_GET sia
di $_POST<?php
// REQUEST_1.php
echo "Ciao " . $_REQUEST['Nome'] . "<br>";
echo "Tu hai " . $_REQUEST['Eta'] . " anni";
?>Inserendo Mario e 17:
Ciao Mario
Tu hai 17 anni
Qualora non vengano inseriti Nome ed Età:
Ciao
Tu hai anni
false in
una condizioneDati incompleti
<!-- REQUEST_POST1.html -->
<form action="REQUEST_1.php" method="POST">
Nome: <input type="text" name="Nome" /> <br>
Età : <input type="text" name="Eta" /> <br><br>
<input type="submit" value="Invia query">
</form>
<!-- REQUEST_POST2.html -->
<form action="REQUEST_2.php" method="POST">
Nome: <input type="text" name="Nome" /> <br>
Età : <input type="text" name="Eta" /> <br><br>
<input type="submit" value="Invia query">
</form>Gli script REQUEST_1.php e REQUEST_2.php non cambiano:
$_REQUEST legge i dati sia da GET sia da
POST.
.php<html>
<!-- PHP_SELF.php -->
<body>
<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>"
method="POST">
Nome: <input type="text" name="Nome" /> <br>
Eta': <input type="text" name="Eta" /> <br><br>
<input type="submit" name="submit" value="Invia query">
</form>
</body>
</html>
<?php
if (isset($_POST['submit'])) // e' stato premuto "Invia query"?
{
echo "Ciao " . $_POST['Nome'] . "<br>";
echo "Tu hai " . $_POST['Eta'] . " anni";
}
?>$_SERVER['PHP_SELF'] contiene il nome
dello script in esecuzionehtmlspecialchars() evita che caratteri
speciali nell'URL vengano interpretati come HTMLname: se è stato premuto, $_POST['submit'] esisteisset() verifica se una variabile (o
una chiave di un array) è definitaaction""=: la form viene inviata alla pagina
corrente| Concetto | Sintassi |
|---|---|
| Form con GET | <form action"s.php"
method="get">= |
| Form con POST | <form action"s.php"
method="post">= |
| Nome del parametro | <input type"text"
name="num1">= |
| Lettura con GET | $_GET['num1'] |
| Concetto | Sintassi |
|---|---|
| Lettura con POST | $_POST['num1'] |
| Lettura GET o POST | $_REQUEST['num1'] |
| Nome dello script | $_SERVER['PHP_SELF'] |
| Verifica invio form | isset($_POST['submit']) |
$_REQUEST: quando lo
script deve accettare dati da entrambi i metodi