mirror of
https://github.com/php/doc-it.git
synced 2026-03-23 23:22:07 +01:00
* Add Italian translations for FAQ section Translate html.xml, passwords.xml, and using.xml to Italian. These files cover PHP/HTML interaction, password hashing security, and common PHP usage questions. Also includes the figures directory with crypt-text-rendered.svg. * Add Italian translations for appendices section Translate examples.xml, transports.xml, comparisons.xml, and history.xml to Italian. These files cover manual examples notes, socket transports, type comparison tables, and PHP history. * Update history.xml * Update html.xml * Update passwords.xml * Update using.xml --------- Co-authored-by: Davide Pastore <pasdavide@gmail.com>
354 lines
12 KiB
XML
354 lines
12 KiB
XML
<?xml version="1.0" encoding="utf-8"?>
|
|
<!-- EN-Revision: 9e6c3416c5c285f807a734e4663c399612777d7e Maintainer: lacatoire Status: ready -->
|
|
<chapter xml:id="faq.html" xmlns="http://docbook.org/ns/docbook">
|
|
<title>PHP e HTML</title>
|
|
<titleabbrev>PHP e HTML</titleabbrev>
|
|
|
|
<para>
|
|
PHP e HTML interagiscono molto: PHP può generare HTML, e HTML
|
|
può passare informazioni a PHP. Prima di leggere queste FAQ, è
|
|
importante imparare come recuperare <link linkend="language.variables.external">
|
|
variabili da fonti esterne</link>. La pagina del manuale su
|
|
questo argomento include anche molti esempi.
|
|
</para>
|
|
|
|
<qandaset>
|
|
<qandaentry xml:id="faq.html.encoding">
|
|
<question>
|
|
<para>
|
|
Quale codifica/decodifica devo usare quando passo un valore attraverso un form/URL?
|
|
</para>
|
|
</question>
|
|
<answer>
|
|
<para>
|
|
Ci sono diverse fasi per le quali la codifica è importante. Supponendo che
|
|
tu abbia una <type>string</type> <varname>$data</varname>, che contiene
|
|
la stringa che vuoi passare in modo non codificato, queste sono le
|
|
fasi rilevanti:
|
|
<itemizedlist>
|
|
<listitem>
|
|
<para>
|
|
Interpretazione HTML. Per specificare una stringa arbitraria,
|
|
<emphasis>devi</emphasis> includerla tra virgolette doppie e
|
|
usare <function>htmlspecialchars</function> sull'intero valore.
|
|
</para>
|
|
</listitem>
|
|
<listitem>
|
|
<para>
|
|
URL: Un URL è composto da diverse parti. Se vuoi che i tuoi dati siano
|
|
interpretati come un singolo elemento, <emphasis>devi</emphasis> codificarli con
|
|
<function>urlencode</function>.
|
|
</para>
|
|
</listitem>
|
|
</itemizedlist>
|
|
</para>
|
|
<para>
|
|
<example>
|
|
<title>Un elemento nascosto di un form HTML</title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
echo '<input type="hidden" value="' . htmlspecialchars($data) . '" />'."\n";
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
</example>
|
|
<note>
|
|
<simpara>
|
|
È sbagliato usare <function>urlencode</function> su
|
|
<varname>$data</varname>, perché è responsabilità del browser
|
|
fare <function>urlencode</function> sui dati. Tutti i browser popolari lo fanno
|
|
correttamente. Nota che questo avverrà indipendentemente dal metodo (cioè,
|
|
GET o POST). Lo noterai solo nel caso di richieste GET però,
|
|
perché le richieste POST sono solitamente nascoste.
|
|
</simpara>
|
|
</note>
|
|
<example>
|
|
<title>Dati da modificare dall'utente</title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
echo "<textarea name='mydata'>\n";
|
|
echo htmlspecialchars($data)."\n";
|
|
echo "</textarea>";
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
</example>
|
|
<note>
|
|
<simpara>
|
|
I dati sono mostrati nel browser come previsto, perché il browser
|
|
interpreterà i simboli HTML con escape.
|
|
</simpara>
|
|
<simpara>
|
|
All'invio, sia tramite GET che POST, i dati saranno codificati URL
|
|
dal browser per il trasferimento, e decodificati direttamente da PHP. Quindi alla
|
|
fine, non hai bisogno di fare alcuna codifica/decodifica URL tu stesso,
|
|
tutto viene gestito automaticamente.
|
|
</simpara>
|
|
</note>
|
|
<example>
|
|
<title>In un URL</title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
echo '<a href="' . htmlspecialchars("/nextpage.php?stage=23&data=" .
|
|
urlencode($data)) . '">'."\n";
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
</example>
|
|
<note>
|
|
<simpara>
|
|
In effetti stai simulando una richiesta HTML GET, quindi è necessario
|
|
fare manualmente <function>urlencode</function> sui dati.
|
|
</simpara>
|
|
</note>
|
|
<note>
|
|
<simpara>
|
|
Devi usare <function>htmlspecialchars</function> sull'intero URL, perché l'URL
|
|
appare come valore di un attributo HTML. In questo caso, il browser
|
|
prima rimuoverà l'escape <function>htmlspecialchars</function> dal valore, e poi passerà
|
|
l'URL. PHP capirà l'URL correttamente, perché hai
|
|
usato <function>urlencode</function> sui dati.
|
|
</simpara>
|
|
<simpara>
|
|
Noterai che il <literal>&</literal> nell'URL è sostituito
|
|
da <literal>&amp;</literal>. Sebbene la maggior parte dei browser sia in grado di interpretare il carattere
|
|
se omesso, non è sempre possibile. Quindi anche se il tuo URL non è
|
|
dinamico, <emphasis>devi</emphasis> usare
|
|
<function>htmlspecialchars</function> sull'URL.
|
|
</simpara>
|
|
</note>
|
|
</para>
|
|
<!-- TODO: a note about addgpcslashes? -->
|
|
</answer>
|
|
</qandaentry>
|
|
|
|
<qandaentry xml:id="faq.html.form-image">
|
|
<question>
|
|
<para>
|
|
Sto cercando di usare un tag <input type="image">, ma
|
|
le variabili <varname>$foo.x</varname> e <varname>$foo.y</varname>
|
|
non sono disponibili. <varname>$_GET['foo.x']</varname> non esiste
|
|
nemmeno. Dove sono?
|
|
</para>
|
|
</question>
|
|
<answer>
|
|
<para>
|
|
Quando si invia un form, è possibile usare un'immagine invece del
|
|
pulsante di invio standard con un tag come:
|
|
<programlisting role="html">
|
|
<![CDATA[
|
|
<input type="image" src="image.gif" name="foo" />
|
|
]]>
|
|
</programlisting>
|
|
Quando l'utente clicca da qualche parte sull'immagine, il form
|
|
corrispondente sarà trasmesso al server con due variabili aggiuntive:
|
|
<varname>foo.x</varname> e <varname>foo.y</varname>.
|
|
</para>
|
|
<para>
|
|
Poiché <varname>foo.x</varname> e <varname>foo.y</varname>
|
|
sarebbero nomi di variabili non validi in PHP, vengono automaticamente convertiti in
|
|
<varname>foo_x</varname> e <varname>foo_y</varname>. Cioè, i
|
|
punti sono sostituiti da underscore. Quindi, accederesti a queste variabili
|
|
come qualsiasi altra descritta nella sezione sul recupero di
|
|
<link linkend="language.variables.external">variabili da fonti
|
|
esterne</link>. Per esempio, <varname>$_GET['foo_x']</varname>.
|
|
<note>
|
|
<para>
|
|
Gli spazi nei nomi delle variabili di richiesta sono convertiti in underscore.
|
|
</para>
|
|
</note>
|
|
</para>
|
|
</answer>
|
|
</qandaentry>
|
|
|
|
<qandaentry xml:id="faq.html.arrays">
|
|
<question>
|
|
<para>Come creo array in un <form> HTML?</para>
|
|
</question>
|
|
<answer>
|
|
<para>
|
|
Per ottenere il risultato del tuo <form> inviato come
|
|
<link linkend="language.types.array">array</link> al tuo script PHP
|
|
nomina gli elementi <input>, <select> o <textarea>
|
|
in questo modo:
|
|
<programlisting role="html">
|
|
<![CDATA[
|
|
<input name="MyArray[]" />
|
|
<input name="MyArray[]" />
|
|
<input name="MyArray[]" />
|
|
<input name="MyArray[]" />
|
|
]]>
|
|
</programlisting>
|
|
Nota le parentesi quadre dopo il nome della variabile, è questo che
|
|
lo rende un array. Puoi raggruppare gli elementi in array diversi
|
|
assegnando lo stesso nome a elementi diversi:
|
|
<programlisting role="html">
|
|
<![CDATA[
|
|
<input name="MyArray[]" />
|
|
<input name="MyArray[]" />
|
|
<input name="MyOtherArray[]" />
|
|
<input name="MyOtherArray[]" />
|
|
]]>
|
|
</programlisting>
|
|
Questo produce due array, MyArray e MyOtherArray, che vengono inviati
|
|
allo script PHP. È anche possibile assegnare chiavi specifiche
|
|
ai tuoi array:
|
|
<programlisting role="html">
|
|
<![CDATA[
|
|
<input name="AnotherArray[]" />
|
|
<input name="AnotherArray[]" />
|
|
<input name="AnotherArray[email]" />
|
|
<input name="AnotherArray[phone]" />
|
|
]]>
|
|
</programlisting>
|
|
L'array AnotherArray conterrà ora le chiavi 0, 1, email e phone.
|
|
</para>
|
|
<para>
|
|
<note>
|
|
<para>
|
|
Specificare le chiavi dell'array è opzionale in HTML. Se non specifichi
|
|
le chiavi, l'array viene riempito nell'ordine in cui gli elementi appaiono nel
|
|
form. Il nostro primo esempio conterrà le chiavi 0, 1, 2 e 3.
|
|
</para>
|
|
</note>
|
|
</para>
|
|
<para>
|
|
Vedi anche
|
|
<link linkend="ref.array">Funzioni Array</link> e
|
|
<link linkend="language.variables.external">Variabili da Fonti
|
|
Esterne</link>.
|
|
</para>
|
|
</answer>
|
|
</qandaentry>
|
|
|
|
<qandaentry xml:id="faq.html.select-multiple">
|
|
<question>
|
|
<para>
|
|
Come ottengo tutti i risultati da un tag HTML select multiple?
|
|
</para>
|
|
</question>
|
|
<answer>
|
|
<para>
|
|
Il tag select multiple in un costrutto HTML permette agli utenti di
|
|
selezionare più elementi da una lista. Questi elementi vengono poi passati
|
|
al gestore dell'azione per il form. Il problema è che vengono
|
|
tutti passati con lo stesso nome del widget. Cioè
|
|
<programlisting role="html">
|
|
<![CDATA[
|
|
<select name="var" multiple="yes">
|
|
]]>
|
|
</programlisting>
|
|
Ogni opzione selezionata arriverà al gestore dell'azione come:
|
|
<programlisting>
|
|
var=option1
|
|
var=option2
|
|
var=option3
|
|
</programlisting>
|
|
Ogni opzione sovrascriverà il contenuto della precedente
|
|
variabile <varname>$var</varname>. La soluzione è usare
|
|
la funzionalità PHP "array da elemento form". Dovrebbe essere
|
|
usato quanto segue:
|
|
<programlisting role="html">
|
|
<![CDATA[
|
|
<select name="var[]" multiple="yes">
|
|
]]>
|
|
</programlisting>
|
|
Questo dice a PHP di trattare <varname>$var</varname> come un array e
|
|
ogni assegnazione di un valore a var[] aggiunge un elemento all'array.
|
|
Il primo elemento diventa <varname>$var[0]</varname>, il successivo
|
|
<varname>$var[1]</varname>, ecc. La funzione <function>count</function>
|
|
può essere usata per determinare quante opzioni sono state selezionate,
|
|
e la funzione <function>sort</function> può essere usata per ordinare
|
|
l'array delle opzioni se necessario.
|
|
</para>
|
|
<para>
|
|
Nota che se stai usando JavaScript le <literal>[]</literal>
|
|
nel nome dell'elemento potrebbero causare problemi quando provi a
|
|
riferirti all'elemento per nome. Usa invece l'ID numerico dell'elemento form,
|
|
o racchiudi il nome della variabile tra apici singoli e
|
|
usalo come indice dell'array elements, per esempio:
|
|
<programlisting>
|
|
variable = document.forms[0].elements['var[]'];
|
|
</programlisting>
|
|
</para>
|
|
</answer>
|
|
</qandaentry>
|
|
|
|
<qandaentry xml:id="faq.html.javascript-variable">
|
|
<question>
|
|
<para>
|
|
Come posso passare una variabile da Javascript a PHP?
|
|
</para>
|
|
</question>
|
|
<answer>
|
|
<para>
|
|
Poiché Javascript è (solitamente) una tecnologia lato client, e
|
|
PHP è (solitamente) una tecnologia lato server, e poiché HTTP è un
|
|
protocollo "stateless", i due linguaggi non possono condividere direttamente
|
|
variabili.
|
|
</para>
|
|
<para>
|
|
È, tuttavia, possibile passare variabili tra i due.
|
|
Un modo per farlo è generare codice Javascript
|
|
con PHP, e far aggiornare il browser, passando variabili specifiche
|
|
allo script PHP. L'esempio sotto mostra
|
|
precisamente come fare questo -- permette al codice PHP di catturare la larghezza
|
|
e l'altezza dello schermo, qualcosa che normalmente è possibile solo
|
|
lato client.
|
|
</para>
|
|
<para>
|
|
<example>
|
|
<title>Generare Javascript con PHP</title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
if (isset($_GET['width']) AND isset($_GET['height'])) {
|
|
// output the geometry variables
|
|
echo "Screen width is: ". $_GET['width'] ."<br />\n";
|
|
echo "Screen height is: ". $_GET['height'] ."<br />\n";
|
|
} else {
|
|
// pass the geometry variables
|
|
// (preserve the original query string
|
|
// -- post variables will need to handled differently)
|
|
|
|
echo "<script language='javascript'>\n";
|
|
echo " location.href=\"{$_SERVER['SCRIPT_NAME']}?{$_SERVER['QUERY_STRING']}"
|
|
. "&width=\" + screen.width + \"&height=\" + screen.height;\n";
|
|
echo "</script>\n";
|
|
exit();
|
|
}
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
</example>
|
|
</para>
|
|
</answer>
|
|
</qandaentry>
|
|
|
|
</qandaset>
|
|
</chapter>
|
|
|
|
<!-- Keep this comment at the end of the file
|
|
Local variables:
|
|
mode: sgml
|
|
sgml-omittag:t
|
|
sgml-shorttag:t
|
|
sgml-minimize-attributes:nil
|
|
sgml-always-quote-attributes:t
|
|
sgml-indent-step:1
|
|
sgml-indent-data:t
|
|
indent-tabs-mode:nil
|
|
sgml-parent-document:nil
|
|
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
|
|
sgml-exposed-tags:nil
|
|
sgml-local-catalogs:nil
|
|
sgml-local-ecat-files:nil
|
|
End:
|
|
vim600: syn=xml fen fdm=syntax fdl=2 si
|
|
vim: et tw=78 syn=sgml
|
|
vi: ts=1 sw=1
|
|
-->
|