php-doc-en/faq/html.xml
Philip Olson c93ad4cde9 Expanded 'Arrays in forms' a bit.
git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@61255 c90b9560-bf6c-de11-be94-00142212c4b1
2001-10-31 08:05:51 +00:00

174 lines
5.4 KiB
XML
Executable file

<?xml encoding="iso-8859-1"?>
<!-- $Revision: 1.7 $ -->
<chapter id="faq.html">
<title>PHP and HTML</title>
<titleabbrev>PHP and HTML</titleabbrev>
<para>
PHP and HTML interact a lot : PHP generate HTML, and HTML
has informations that will be sent to PHP.
</para>
<qandaset>
<qandaentry id="faq.html.form-image">
<question>
<para>
I'm trying to use an &lt;input type="image"&gt; tag, but
the $foo.x and $foo.y variables aren't available.
Where are they?
</para>
</question>
<answer>
<para>
When submitting a form, it is possible to use an image instead of
the standard submit button with a tag like:
<programlisting>
<![CDATA[
<input type="image" src="image.gif" name="foo">
]]>
</programlisting>
When the user clicks somewhere on the image, the accompanying form
will be transmitted to the server with two additional variables:
foo.x and foo.y.
</para>
<para>
Because $foo.x and $foo.y are invalid variable names in PHP, they are
automagically converted to $foo_x and $foo_y. That is, the periods
are replaced with underscores.
</para>
</answer>
</qandaentry>
<qandaentry id="faq.html.arrays">
<question>
<para>How do I create arrays in a HTML &lt;form&gt;?</para>
</question>
<answer>
<para>
To get your &lt;form&gt; result sent as an
<link linkend="language.types.array">array</link> to your PHP script
you name the &lt;input&gt;, &lt;select&gt; or &lt;textarea&gt;
elements like this:
<programlisting>
<![CDATA[
<input name="MyArray[]">
<input name="MyArray[]">
<input name="MyArray[]">
<input name="MyArray[]">
]]>
</programlisting>
Notice the square brackets after the variable name, that's what
makes it an array. You can group the elements into different arrays
by assigning the same name to different elements:
<programlisting>
<![CDATA[
<input name="MyArray[]">
<input name="MyArray[]">
<input name="MyOtherArray[]">
<input name="MyOtherArray[]">
]]>
</programlisting>
This produces two arrays, MyArray and MyOtherArray, that gets sent
to the PHP script. It's also possible to assign specific keys
to your arrays:
<programlisting>
<![CDATA[
<input name="AnotherArray[]">
<input name="AnotherArray[]">
<input name="AnotherArray[email]">
<input name="AnotherArray[phone]">
]]>
</programlisting>
The AnotherArray array will now contain the keys 0, 1, email and phone.
</para>
<para>
<note>
<para>
Specifying an arrays key is optional in HTML. If you do not specify
the keys, the array gets filled in the order the elements appear in
the form. Our first example will contain keys 0, 1, 2 and 3.
</para>
</note>
</para>
<para>
See also
<link linkend="ref.array">Array Functions</link> and
<link linkend="language.variables.external">Variables from outside 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>
<![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>
<![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
sgml-parent-document:nil
sgml-default-dtd-file:"../../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
-->