mirror of
https://github.com/sigmasternchen/php-doc-en
synced 2025-03-15 16:38:54 +00:00

Spreading the content of common.xml among several files git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@59916 c90b9560-bf6c-de11-be94-00142212c4b1
165 lines
5.1 KiB
XML
Executable file
165 lines
5.1 KiB
XML
Executable file
<?xml encoding="iso-8859-1"?>
|
|
<!-- $Revision: 1.6 $ -->
|
|
<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 <input type="image"> 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 <form>?</para>
|
|
</question>
|
|
<answer>
|
|
<para>
|
|
To get your <form> result sent as an array to your PHP script
|
|
you name the <input>, <select> or <textarea>
|
|
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.
|
|
</para>
|
|
<para>
|
|
<note>
|
|
<para>
|
|
You need not use indices with arrays in HTML, although you can
|
|
specify number or associative indices, so you can exactly specify
|
|
what indices will contain the information provided by that
|
|
input element. If you do not specify indices, the array gets
|
|
filled in the order the elements appear in the form.
|
|
</para>
|
|
</note>
|
|
</para>
|
|
<para>
|
|
For functions you can use to process these arrays once you get
|
|
them into your scripts, please see the
|
|
<link linkend="ref.array">Arrays section</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
|
|
-->
|