luk Sun Mar 10 16:57:25 2002 EDT
Added files:
/phpdoc/cs/faq html.xml
Log:
Index: phpdoc/cs/faq/html.xml
+++ phpdoc/cs/faq/html.xml
<?xml version="1.0" encoding="iso-8859-2"?>
<!-- EN-Revision: 1.13 Maintainer: luk Status: ready -->
<chapter id="faq.html">
<title>PHP a HTML</title>
<titleabbrev>PHP a HTML</titleabbrev>
<para>
PHP a HTML maj� hodn� spole�n�ho: PHP generuje HTML, a HTML m� informace,
kter� budou posl�ny PHP.
</para>
<qandaset>
<qandaentry id="faq.html.encoding">
<question>
<para>
Jak� zak�dov�n�/dek�dov�n� pot�ebuji, kdy� pos�l�m hodnotu p�es
formul��? A v URL?
</para>
</question>
<answer>
<para>
Je v�ce situac�, pro kter� je zak�dov�n� d�le�it�. Za p�edpokladu, �e
m�te <type>string</type> <varname>$data</varname>, kter� obsahuje
�et�zec, jen� m�te nezak�dovan� a chcete ho poslat, je t�eba se zab�vat
t�mito relevantn�mi probl�my:
<itemizedlist>
<listitem>
<para>
HTML interpretace. Pokud specifikujete n�hodn� (obecn�) �et�zec,
<emphasis>mus�te</emphasis> ho d�t do uvozovek a cel� ho zpracovat
funkc� <function>htmlspecialchars</function> (aby se
odstranily/p�evedly speci�ln� znaky jazyka HTML).
</para>
</listitem>
<listitem>
<para>
URL: sest�v� z n�kolika ��st�. Pokud chcete, aby va�e data byla
interpretov�na jako jedna polo�ka, <emphasis>mus�te</emphasis>
je zak�dovat pomoc� <function>urlencode</function>.
</para>
</listitem>
</itemizedlist>
</para>
<para>
<example>
<title>Skryt� element HTML formul��e</title>
<programlisting role="php">
<![CDATA[
<?php
echo "<input type=hidden value=\"" . htmlspecialchars($data) . "\">\n";
?>
]]>
</programlisting>
</example>
<note>
<simpara>
Je chybou pou��t <function>urlencode</function> pro
<varname>$data</varname>, proto�e prohl��e�e samy zaji��uj�
zpracov�n� dat shodn� s funkc� <function>urlencode</function>.
V�echny obl�ben� prohl��e�e to d�laj� korektn�. Uv�domte si, �e
toto nen� z�visl� na pou�it� metod� (nap�. GET nebo POST).
V�imnete si toho v�ak pouze v p��pad� GET, proto�e po�adavky POST
jsou obvykle skryt�.
</simpara>
</note>
<example>
<title>Data k editaci u�ivatelem</title>
<programlisting role="php">
<![CDATA[
<?php
echo "<textarea name=mydata>\n";
echo htmlspecialchars($data)."\n";
echo "</textarea>";
?>
]]>
</programlisting>
</example>
<note>
<simpara>
Data jsou v prohl��e�i zobrazena tak, jak bylo zam��leno, proto�e
prohl��e� bude spr�vn� interpretovat speci�ln� symboly.
</simpara>
<simpara>
Po odesl�n�, a� ji� pomoc� GET nebo POST, data budou zak�dov�na
zp�sobem urlencode pro p�enos a n�sledn� p��mo dek�dov�na v PHP.
Tak�e v�bec nepot�ebujete prov�d�t ��dn� zak�dov�n�/dek�dov�n� ru�n�,
v�e je prov�d�no automaticky.
</simpara>
</note>
<example>
<title>Uvnit� URL</title>
<programlisting role="php">
<![CDATA[
<?php
echo "<a href=\"" . htmlspecialchars("/nexpage.php?stage=23&data=" .
urlencode($data)) . "\">\n";
?>
]]>
</programlisting>
</example>
<note>
<simpara>
V tomto p��pad� ji� opravdu vytv���te GET po�adavek, proto je nutn�
data k�dovat ru�n� pomoc� <function>urlencode</function>.
</simpara>
</note>
<note>
<simpara>
Mus�te tak� pou��t <function>htmlspecialchars</function> na cel� URL,
proto�e URL je zde hodnotou HTML atributu. V tomto p��pad� prohl��e�
nejd��ve odstran� speci�ln� znaky a pak zpracuje URL. PHP spr�vn�
pochop� pos�lan� URL, proto�e jste data zak�dovali pomoc�
<function>urlencoded</function>.
</simpara>
<simpara>
M��ete se v�imnout, �e symbol <literal>&</literal> v URL je
nahrazen <literal>&amp;</literal>. P�esto�e to v�t�ina prohl��e��
oprav�. pokud na to zapomenete, nen� to v�dy mo�n�. Tak�e pokud
v� URL nen� dynamick�, <emphasis>mus�te</emphasis> pou��t
<function>htmlspecialchars</function>.
</simpara>
</note>
</para>
<!-- TODO: a note about addgpcslashes? -->
</answer>
</qandaentry>
<qandaentry id="faq.html.form-image">
<question>
<para>
Zkou��m pou��t tag <input type="image">, ale prom�nn�
$foo.x a $foo.y nejsou dostupn�. Kde jsou?
</para>
</question>
<answer>
<para>
Kdy� odes�l�te formul��, lze nam�sto standardn�ho tla��tka pou��t
obr�zek pomoc� tagu jako
<programlisting role="html">
<![CDATA[
<input type="image" src="image.gif" name="foo">
]]>
</programlisting>
Kdy� u�ivatel klikne n�kde na obr�zku, p��slu�n� formul�� se ode�le
na server s dv�ma prom�nn�mi nav�c: foo.x a foo.y.
</para>
<para>
Proto�e $foo.x a $foo.y jsou v PHP neplatn� n�zvy prom�nn�ch, jsou
automaticky p�evedeny na $foo_x a $foo_y. Tzn. te�ky jsou nahrazeny
podtr��tky.
</para>
</answer>
</qandaentry>
<qandaentry id="faq.html.arrays">
<question>
<para>Jak vytvo��m pole ("array") v HTML formul��i?</para>
</question>
<answer>
<para>
Aby v�sledky odesl�n� va�eho formul��e byly um�st�ny v poli
(<link linkend="language.types.array">array</link>), nazv�te
elementy <input>, <select> nebo <textarea>
t�mto zp�sobem:
<programlisting role="html">
<![CDATA[
<input name="MyArray[]">
<input name="MyArray[]">
<input name="MyArray[]">
<input name="MyArray[]">
]]>
</programlisting>
V�imn�te si hranat�ch z�vorek po n�zvu prom�nn�, to je to, co z toho
d�l� pole. M��ete seskupovat elementy do r�zn�ch pol� spojen�m
stejn�ho jm�na s r�zn�mi elementy:
<programlisting role="html">
<![CDATA[
<input name="MyArray[]">
<input name="MyArray[]">
<input name="MyOtherArray[]">
<input name="MyOtherArray[]">
]]>
</programlisting>
Toto produkuje dv� pole, MyArray a MyOtherArray, kter� budou zasl�na
PHP skriptu. Je tak� mo�n� d�t do pol� specifick� kl��e:
<programlisting role="html">
<![CDATA[
<input name="AnotherArray[]">
<input name="AnotherArray[]">
<input name="AnotherArray[email]">
<input name="AnotherArray[phone]">
]]>
</programlisting>
Pole AnotherArray bude nyn� obsahovat kl��e 0, 1, email a phone.
</para>
<para>
<note>
<para>
Specifikace kl�c� pol� je v HTML nepovinn�. Pokud kl��e
nespecifikujete, pole bude vypln�no podle po�ad� element� ve
formul��i. N� prvn� p��klad obsahuje kl��e 0, 1, 2 a 3.
</para>
</note>
</para>
<para>
Viz tak�
<link linkend="ref.array">Funkce pro pr�ci s poli</link> a
<link linkend="language.variables.external">Prom�nn� z vn�j�ku PHP</link>.
</para>
</answer>
</qandaentry>
<qandaentry id="faq.html.select-multiple">
<question>
<para>
How do I get all the results from a select multiple HTML tag?
</para>
</question>
<answer>
<para>
The select multiple tag in an HTML construct allows users to
select multiple items from a list. These items are then passed
to the action handler for the form. The problem is that they
are all passed with the same widget name. ie.
<programlisting role="html">
<![CDATA[
<select name="var" multiple>
]]>
</programlisting>
Each selected option will arrive at the action handler as:
<programlisting>
var=option1
var=option2
var=option3
</programlisting>
Each option will overwrite the contents of the previous
<varname>$var</varname> variable. The solution is to use
PHP's "array from form element" feature. The following
should be used:
<programlisting role="html">
<![CDATA[
<select name="var[]" multiple>
]]>
</programlisting>
This tells PHP to treat <varname>$var</varname> as an array and
each assignment of a value to var[] adds an item to the array.
The first item becomes <varname>$var[0]</varname>, the next
<varname>$var[1]</varname>, etc. The <function>count</function>
function can be used to determine how many options were selected,
and the <function>sort</function> function can be used to sort
the option array if necessary.
</para>
<para>
Note that if you are using JavaScript the <literal>[]</literal>
on the element name might cause you problems when you try to
refer to the element by name. Use it's numerical form element
id instead, or enclose the variable name in single quotes and
use that as the index to the elements array, for example:
<programlisting>
variable = documents.forms[0].elements['var[]'];
</programlisting>
</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:"../manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
-->