phpday 2027 - Call for Papers

Trattare con i Form

Una delle caratteristiche più potenti di PHP è il modo in cui gestisce i form HTML. Il concetto di base da comprendere è che qualsiasi elemento di un form sarà automaticamente disponibile negli script PHP. Si consulti la sezione del manuale sulle Variabili da sorgenti esterne per maggiori informazioni ed esempi sull'utilizzo dei form con PHP. Ecco un esempio di form HTML:

Example #1 Un semplice form HTML

<form action="action.php" method="post">
    <label for="name">Il tuo nome:</label>
    <input name="name" id="name" type="text">

    <label for="age">La tua età:</label>
    <input name="age" id="age" type="number">

    <button type="submit">Invia</button>
</form>

Questo form non ha niente di speciale. È un semplice form HTML senza alcun tipo di tag particolare. Quando l'utente compila questo form e preme il pulsante di invio, viene richiamata la pagina action.php. In questo file si scriverà qualcosa di simile:

Example #2 Stampare i dati dal nostro form

Ciao <?php echo htmlspecialchars($_POST['name']); ?>.
La tua età è di <?php echo (int) $_POST['age']; ?> anni.

Un possibile output di questo script potrebbe essere:

Ciao Joe. La tua età è di 22 anni.

A parte le parti relative a htmlspecialchars() e (int), ciò che avviene dovrebbe risultare ovvio. htmlspecialchars() si assicura che qualsiasi carattere speciale in HTML sia propriamente codificato, in modo che non si possano iniettare tag HTML o JavaScript nella pagina. Per il campo age, dato che sappiamo che è un numero, possiamo semplicemente convertirlo in un int, il che eliminerà automaticamente qualsiasi carattere estraneo. È anche possibile far fare questo automaticamente a PHP utilizzando l'estensione filter. Le variabili $_POST['name'] e $_POST['age'] vengono impostate automaticamente da PHP. In precedenza abbiamo usato la superglobale $_SERVER; qui abbiamo appena introdotto la superglobale $_POST che contiene tutti i dati POST. Notare che il metodo del nostro form è POST. Se usassimo il metodo GET, le informazioni del nostro form si troverebbero invece nella superglobale $_GET. Si può anche usare la superglobale $_REQUEST se la provenienza dei dati della richiesta non interessa. Essa contiene l'unione dei dati GET, POST e COOKIE.

+add a note

User Contributed Notes 1 note

up
50
sethg at ropine dot com
22 years ago
According to the HTTP specification, you should use the POST method when you're using the form to change the state of something on the server end. For example, if a page has a form to allow users to add their own comments, like this page here, the form should use POST. If you click "Reload" or "Refresh" on a page that you reached through a POST, it's almost always an error -- you shouldn't be posting the same comment twice -- which is why these pages aren't bookmarked or cached.

You should use the GET method when your form is, well, getting something off the server and not actually changing anything.  For example, the form for a search engine should use GET, since searching a Web site should not be changing anything that the client might care about, and bookmarking or caching the results of a search-engine query is just as useful as bookmarking or caching a static HTML page.
To Top