2001-11-10 21:49:43 +00:00
|
|
|
<?xml version="1.0" encoding="iso-8859-1"?>
|
2003-08-17 19:09:25 +00:00
|
|
|
<!-- $Revision: 1.22 $ -->
|
2001-07-20 18:12:03 +00:00
|
|
|
<chapter id="faq.html">
|
|
|
|
<title>PHP and HTML</title>
|
|
|
|
<titleabbrev>PHP and HTML</titleabbrev>
|
|
|
|
|
2001-10-14 10:41:34 +00:00
|
|
|
<para>
|
2002-06-30 10:41:44 +00:00
|
|
|
PHP and HTML interact a lot: PHP can generate HTML, and HTML
|
2003-01-24 19:46:14 +00:00
|
|
|
can pass information to PHP. Before reading these faqs, it's
|
|
|
|
important you learn how to <link linkend="language.variables.external">
|
|
|
|
retrieve variables from outside of PHP</link>. The manual page on
|
|
|
|
this topic includes many examples as well. Pay close attention to
|
|
|
|
what <literal>register_globals</literal> means to you too.
|
2001-10-14 10:41:34 +00:00
|
|
|
</para>
|
2001-07-20 18:12:03 +00:00
|
|
|
|
|
|
|
<qandaset>
|
2001-11-06 20:51:27 +00:00
|
|
|
<qandaentry id="faq.html.encoding">
|
|
|
|
<question>
|
|
|
|
<para>
|
2002-06-30 10:41:44 +00:00
|
|
|
What encoding/decoding do I need when I pass a value through a form/URL?
|
2001-11-06 20:51:27 +00:00
|
|
|
</para>
|
|
|
|
</question>
|
|
|
|
<answer>
|
|
|
|
<para>
|
|
|
|
There are several stages for which encoding is important. Assuming that
|
|
|
|
you have a <type>string</type> <varname>$data</varname>, which contains
|
|
|
|
the string you want to pass on in a non-encoded way, these are the
|
|
|
|
relevant stages:
|
|
|
|
<itemizedlist>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
HTML interpretation. In order to specify a random string, you
|
2002-06-30 10:41:44 +00:00
|
|
|
<emphasis>must</emphasis> include it in double quotes, and
|
2003-01-24 17:08:37 +00:00
|
|
|
<function>htmlspecialchars</function> the whole value.
|
2001-11-06 20:51:27 +00:00
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
URL: A URL consists of several parts. If you want your data to be
|
|
|
|
interpreted as one item, you <emphasis>must</emphasis> encode it with
|
|
|
|
<function>urlencode</function>.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</itemizedlist>
|
|
|
|
</para>
|
|
|
|
<para>
|
|
|
|
<example>
|
|
|
|
<title>A hidden HTML form element</title>
|
|
|
|
<programlisting role="php">
|
|
|
|
<![CDATA[
|
|
|
|
<?php
|
2003-08-17 19:09:25 +00:00
|
|
|
echo "<input type='hidden' value='" . htmlspecialchars($data) . "'>\n";
|
2001-11-06 20:51:27 +00:00
|
|
|
?>
|
|
|
|
]]>
|
|
|
|
</programlisting>
|
|
|
|
</example>
|
|
|
|
<note>
|
|
|
|
<simpara>
|
|
|
|
It is wrong to <function>urlencode</function>
|
2002-03-28 16:32:59 +00:00
|
|
|
<varname>$data</varname>, because it's the browsers responsibility to
|
2001-11-06 20:51:27 +00:00
|
|
|
<function>urlencode</function> the data. All popular browsers do that
|
|
|
|
correctly. Note that this will happen regardless of the method (i.e.,
|
|
|
|
GET or POST). You'll only notice this in case of GET request though,
|
|
|
|
because POST requests are usually hidden.
|
|
|
|
</simpara>
|
|
|
|
</note>
|
2001-11-06 20:59:14 +00:00
|
|
|
<example>
|
|
|
|
<title>Data to be edited by the user</title>
|
|
|
|
<programlisting role="php">
|
|
|
|
<![CDATA[
|
|
|
|
<?php
|
2003-08-17 19:09:25 +00:00
|
|
|
echo "<textarea name='mydata'>\n";
|
2002-01-09 00:29:37 +00:00
|
|
|
echo htmlspecialchars($data)."\n";
|
2001-11-06 20:59:14 +00:00
|
|
|
echo "</textarea>";
|
|
|
|
?>
|
|
|
|
]]>
|
|
|
|
</programlisting>
|
|
|
|
</example>
|
|
|
|
<note>
|
|
|
|
<simpara>
|
|
|
|
The data is shown in the browser as intended, because the browser will
|
2002-06-30 10:41:44 +00:00
|
|
|
interpret the HTML escaped symbols.
|
2001-11-06 20:59:14 +00:00
|
|
|
</simpara>
|
|
|
|
<simpara>
|
|
|
|
Upon submitting, either via GET or POST, the data will be urlencoded
|
|
|
|
by the browser for transferring, and directly urldecoded by PHP. So in
|
|
|
|
the end, you don't need to do any urlencoding/urldecoding yourself,
|
2002-03-28 16:06:33 +00:00
|
|
|
everything is handled automagically.
|
2001-11-06 20:59:14 +00:00
|
|
|
</simpara>
|
|
|
|
</note>
|
2001-11-06 20:51:27 +00:00
|
|
|
<example>
|
|
|
|
<title>In an URL</title>
|
|
|
|
<programlisting role="php">
|
|
|
|
<![CDATA[
|
|
|
|
<?php
|
2003-08-17 19:09:25 +00:00
|
|
|
echo "<a href='" . htmlspecialchars("/nextpage.php?stage=23&data=" .
|
|
|
|
urlencode($data)) . "'>\n";
|
2001-11-06 20:51:27 +00:00
|
|
|
?>
|
|
|
|
]]>
|
|
|
|
</programlisting>
|
|
|
|
</example>
|
|
|
|
<note>
|
|
|
|
<simpara>
|
|
|
|
In fact you are faking a HTML GET request, therefore it's necessary to
|
|
|
|
manually <function>urlencode</function> the data.
|
|
|
|
</simpara>
|
|
|
|
</note>
|
|
|
|
<note>
|
|
|
|
<simpara>
|
2002-01-09 00:29:37 +00:00
|
|
|
You need to <function>htmlspecialchars</function> the whole URL, because the
|
2001-11-06 20:51:27 +00:00
|
|
|
URL occurs as value of an HTML-attribute. In this case, the browser
|
2002-01-09 00:29:37 +00:00
|
|
|
will first un-<function>htmlspecialchars</function> the value, and then pass
|
2002-03-28 16:06:33 +00:00
|
|
|
the URL on. PHP will understand the URL correctly, because you
|
2001-11-06 20:51:27 +00:00
|
|
|
<function>urlencoded</function> the data.
|
|
|
|
</simpara>
|
|
|
|
<simpara>
|
|
|
|
You'll notice that the <literal>&</literal> in the URL is replaced
|
|
|
|
by <literal>&amp;</literal>. Although most browsers will recover
|
|
|
|
if you forget this, this isn't always possible. So even if your URL is
|
|
|
|
not dynamic, you <emphasis>need</emphasis> to
|
2002-01-09 00:29:37 +00:00
|
|
|
<function>htmlspecialchars</function> the URL.
|
2001-11-06 20:51:27 +00:00
|
|
|
</simpara>
|
|
|
|
</note>
|
|
|
|
</para>
|
2001-11-06 20:59:14 +00:00
|
|
|
<!-- TODO: a note about addgpcslashes? -->
|
2001-11-06 20:51:27 +00:00
|
|
|
</answer>
|
|
|
|
</qandaentry>
|
|
|
|
|
2001-10-14 11:55:30 +00:00
|
|
|
<qandaentry id="faq.html.form-image">
|
|
|
|
<question>
|
|
|
|
<para>
|
|
|
|
I'm trying to use an <input type="image"> tag, but
|
2003-01-24 19:46:14 +00:00
|
|
|
the <varname>$foo.x</varname> and <varname>$foo.y</varname> variables
|
|
|
|
aren't available. <varname>$_GET['foo.x']</varname> isn't existing
|
|
|
|
either. Where are they?
|
2001-10-14 11:55:30 +00:00
|
|
|
</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:
|
2001-11-09 15:48:16 +00:00
|
|
|
<programlisting role="html">
|
2001-10-14 11:55:30 +00:00
|
|
|
<![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:
|
2003-01-24 17:08:37 +00:00
|
|
|
<varname>foo.x</varname> and <varname>foo.y</varname>.
|
2001-10-14 11:55:30 +00:00
|
|
|
</para>
|
|
|
|
<para>
|
2003-01-24 19:46:14 +00:00
|
|
|
Because <varname>foo.x</varname> and <varname>foo.y</varname> would
|
|
|
|
make invalid variable names in PHP, they are automagically converted to
|
|
|
|
<varname>foo_x</varname> and <varname>foo_y</varname>. That is, the
|
|
|
|
periods are replaced with underscores. So, you'd access these variables
|
|
|
|
like any other described within the section on retrieving
|
|
|
|
<link linkend="language.variables.external">variables from outside of
|
|
|
|
PHP</link>. For example, <varname>$_GET['foo_x']</varname>.
|
2001-10-14 11:55:30 +00:00
|
|
|
</para>
|
|
|
|
</answer>
|
|
|
|
</qandaentry>
|
|
|
|
|
2001-07-20 18:12:03 +00:00
|
|
|
<qandaentry id="faq.html.arrays">
|
|
|
|
<question>
|
|
|
|
<para>How do I create arrays in a HTML <form>?</para>
|
|
|
|
</question>
|
|
|
|
<answer>
|
|
|
|
<para>
|
2001-10-31 08:05:51 +00:00
|
|
|
To get your <form> result sent as an
|
|
|
|
<link linkend="language.types.array">array</link> to your PHP script
|
2001-07-20 18:12:03 +00:00
|
|
|
you name the <input>, <select> or <textarea>
|
|
|
|
elements like this:
|
2001-11-09 15:48:16 +00:00
|
|
|
<programlisting role="html">
|
2001-10-14 10:41:34 +00:00
|
|
|
<![CDATA[
|
|
|
|
<input name="MyArray[]">
|
|
|
|
<input name="MyArray[]">
|
|
|
|
<input name="MyArray[]">
|
|
|
|
<input name="MyArray[]">
|
|
|
|
]]>
|
2001-07-20 18:12:03 +00:00
|
|
|
</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:
|
2001-11-09 15:48:16 +00:00
|
|
|
<programlisting role="html">
|
2001-10-14 10:41:34 +00:00
|
|
|
<![CDATA[
|
|
|
|
<input name="MyArray[]">
|
|
|
|
<input name="MyArray[]">
|
|
|
|
<input name="MyOtherArray[]">
|
|
|
|
<input name="MyOtherArray[]">
|
|
|
|
]]>
|
2001-07-20 18:12:03 +00:00
|
|
|
</programlisting>
|
|
|
|
This produces two arrays, MyArray and MyOtherArray, that gets sent
|
2003-04-08 11:31:40 +00:00
|
|
|
to the PHP script. It's also possible to assign specific keys
|
2001-10-31 08:05:51 +00:00
|
|
|
to your arrays:
|
2001-11-09 15:48:16 +00:00
|
|
|
<programlisting role="html">
|
2001-10-31 08:05:51 +00:00
|
|
|
<![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.
|
2001-07-20 18:12:03 +00:00
|
|
|
</para>
|
|
|
|
<para>
|
2001-10-14 10:41:34 +00:00
|
|
|
<note>
|
|
|
|
<para>
|
2003-04-08 11:31:40 +00:00
|
|
|
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
|
2001-10-31 08:05:51 +00:00
|
|
|
the form. Our first example will contain keys 0, 1, 2 and 3.
|
2001-10-14 10:41:34 +00:00
|
|
|
</para>
|
|
|
|
</note>
|
|
|
|
</para>
|
|
|
|
<para>
|
2003-04-08 11:31:40 +00:00
|
|
|
See also
|
|
|
|
<link linkend="ref.array">Array Functions</link> and
|
2001-10-31 08:05:51 +00:00
|
|
|
<link linkend="language.variables.external">Variables from outside PHP</link>.
|
2001-07-20 18:12:03 +00:00
|
|
|
</para>
|
|
|
|
</answer>
|
|
|
|
</qandaentry>
|
|
|
|
|
2001-10-14 11:55:30 +00:00
|
|
|
<qandaentry id="faq.html.select-multiple">
|
|
|
|
<question>
|
|
|
|
<para>
|
2003-04-08 11:31:40 +00:00
|
|
|
How do I get all the results from a select multiple HTML tag?
|
2001-10-14 11:55:30 +00:00
|
|
|
</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.
|
2001-11-09 15:48:16 +00:00
|
|
|
<programlisting role="html">
|
2001-10-14 11:55:30 +00:00
|
|
|
<![CDATA[
|
2003-08-17 19:09:25 +00:00
|
|
|
<select name="var" multiple="yes">
|
2001-10-14 11:55:30 +00:00
|
|
|
]]>
|
|
|
|
</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:
|
2001-11-09 15:48:16 +00:00
|
|
|
<programlisting role="html">
|
2001-10-14 11:55:30 +00:00
|
|
|
<![CDATA[
|
2003-08-17 19:09:25 +00:00
|
|
|
<select name="var[]" multiple="yes">
|
2001-10-14 11:55:30 +00:00
|
|
|
]]>
|
|
|
|
</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
|
2002-06-30 10:41:44 +00:00
|
|
|
ID instead, or enclose the variable name in single quotes and
|
2001-10-14 11:55:30 +00:00
|
|
|
use that as the index to the elements array, for example:
|
|
|
|
<programlisting>
|
|
|
|
variable = documents.forms[0].elements['var[]'];
|
|
|
|
</programlisting>
|
|
|
|
</para>
|
|
|
|
</answer>
|
|
|
|
</qandaentry>
|
2003-04-08 11:31:40 +00:00
|
|
|
|
|
|
|
<qandaentry id="faq.html.javascript-variable">
|
|
|
|
<question>
|
|
|
|
<para>
|
|
|
|
How can I pass a variable from Javascript to PHP?
|
|
|
|
</para>
|
|
|
|
</question>
|
|
|
|
<answer>
|
|
|
|
<para>
|
|
|
|
Since Javascript is (usually) a client-side technology, and
|
|
|
|
PHP is (usually) a server-side technology, and since HTTP is a
|
|
|
|
"stateless" protocol, the two languages cannot directly share
|
|
|
|
variables.
|
|
|
|
</para>
|
|
|
|
<para>
|
|
|
|
It is, however, possible to pass variables between the two.
|
|
|
|
One way of accomplishing this is to generate Javascript code
|
2003-04-09 03:22:19 +00:00
|
|
|
with PHP, and have the browser refresh itself, passing specific
|
2003-04-08 11:31:40 +00:00
|
|
|
variables back to the PHP script. The example below shows
|
|
|
|
precisely how to do this -- it allows PHP code to capture screen
|
|
|
|
height and width, something that is normally only possible on
|
|
|
|
the client side.
|
|
|
|
</para>
|
|
|
|
<para>
|
|
|
|
<programlisting>
|
|
|
|
<![CDATA[
|
|
|
|
<?php
|
2003-04-09 03:22:19 +00:00
|
|
|
if (isset($_GET['width']) AND isset($_GET['height'])) {
|
2003-04-08 11:31:40 +00:00
|
|
|
// output the geometry variables
|
|
|
|
echo "Screen width is: ". $_GET['width'] ."<br />\n";
|
|
|
|
echo "Screen height is: ". $_GET['height'] ."<br />\n";
|
2003-04-09 03:22:19 +00:00
|
|
|
} else {
|
2003-04-08 11:31:40 +00:00
|
|
|
// pass the geometry variables
|
|
|
|
// (preserve the original query string
|
|
|
|
// -- post variables will need to handled differently)
|
|
|
|
|
2003-08-17 19:09:25 +00:00
|
|
|
echo "<script language='javascript'>\n";
|
2003-04-08 11:31:40 +00:00
|
|
|
echo " location.href=\"${_SERVER['SCRIPT_NAME']}?${_SERVER['QUERY_STRING']}"
|
|
|
|
. "&width=\" + screen.width + \"&height=\" + screen.height;\n";
|
|
|
|
echo "</script>\n";
|
|
|
|
exit();
|
|
|
|
}
|
|
|
|
?>
|
|
|
|
]]>
|
|
|
|
</programlisting>
|
|
|
|
</para>
|
|
|
|
</answer>
|
|
|
|
</qandaentry>
|
|
|
|
|
2001-07-20 18:12:03 +00:00
|
|
|
</qandaset>
|
|
|
|
</chapter>
|
|
|
|
|
2001-09-21 22:47:49 +00:00
|
|
|
<!-- 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
|
2001-12-12 20:47:43 +00:00
|
|
|
indent-tabs-mode:nil
|
2001-09-21 22:47:49 +00:00
|
|
|
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
|
|
|
|
-->
|