Source of: /manual/en/faq.html.php
<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/faq.inc";
$setup = array (
'home' =>
array (
0 => 'index.php',
1 => 'PHP Manual',
),
'head' =>
array (
0 => 'UTF-8',
1 => 'en',
),
'this' =>
array (
0 => 'faq.html.php',
1 => 'PHP and HTML',
),
'up' =>
array (
0 => 'faq.php',
1 => 'FAQ',
),
'prev' =>
array (
0 => 'faq.passwords.php',
1 => 'Password Hashing',
),
'next' =>
array (
0 => 'faq.com.php',
1 => 'PHP and COM',
),
'alternatives' =>
array (
),
);
$setup["toc"] = $TOC;
$setup["parents"] = $PARENTS;
manual_setup($setup);
manual_header();
?>
<div id="faq.html" class="chapter">
<h1>PHP and HTML</h1>
<p class="para">
PHP and HTML interact a lot: PHP can generate HTML, and HTML
can pass information to PHP. Before reading these faqs, it's
important you learn how to retrieve <a href="language.variables.external.php" class="link">
variables from external sources</a>. The manual page on
this topic includes many examples as well. Pay close attention to
what <em>register_globals</em> means to you too.
</p>
<div class="qandaset"><ol class="qandaset_questions"><li><a href="#faq.html.encoding">
What encoding/decoding do I need when I pass a value through a form/URL?
</a></li><li><a href="#faq.html.form-image">
I'm trying to use an <input type="image"> tag, but
the $foo.x and $foo.y variables
aren't available. $_GET['foo.x'] isn't existing
either. Where are they?
</a></li><li><a href="#faq.html.arrays">
How do I create arrays in a HTML <form>?
</a></li><li><a href="#faq.html.select-multiple">
How do I get all the results from a select multiple HTML tag?
</a></li><li><a href="#faq.html.javascript-variable">
How can I pass a variable from Javascript to PHP?
</a></li></ol></div>
<dl class="qandaentry" id="faq.html.encoding">
<dt><strong>
What encoding/decoding do I need when I pass a value through a form/URL?
</strong></dt>
<dd class="answer">
<p class="para">
There are several stages for which encoding is important. Assuming that
you have a <span class="type"><a href="language.types.string.php" class="type string">string</a></span> <var class="varname"><var class="varname">$data</var></var>, which contains
the string you want to pass on in a non-encoded way, these are the
relevant stages:
<ul class="itemizedlist">
<li class="listitem">
<p class="para">
HTML interpretation. In order to specify a random string, you
<em class="emphasis">must</em> include it in double quotes, and
<span class="function"><a href="function.htmlspecialchars.php" class="function">htmlspecialchars()</a></span> the whole value.
</p>
</li>
<li class="listitem">
<p class="para">
URL: A URL consists of several parts. If you want your data to be
interpreted as one item, you <em class="emphasis">must</em> encode it with
<span class="function"><a href="function.urlencode.php" class="function">urlencode()</a></span>.
</p>
</li>
</ul>
</p>
<p class="para">
<div class="example" id="example-5463">
<p><strong>Example #1 A hidden HTML form element</strong></p>
<div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB"><?php<br /> </span><span style="color: #007700">echo </span><span style="color: #DD0000">'<input type="hidden" value="' </span><span style="color: #007700">. </span><span style="color: #0000BB">htmlspecialchars</span><span style="color: #007700">(</span><span style="color: #0000BB">$data</span><span style="color: #007700">) . </span><span style="color: #DD0000">'" />'</span><span style="color: #007700">.</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?></span>
</span>
</code></div>
</div>
</div>
<blockquote class="note"><p><strong class="note">Note</strong>:
<span class="simpara">
It is wrong to <span class="function"><a href="function.urlencode.php" class="function">urlencode()</a></span>
<var class="varname"><var class="varname">$data</var></var>, because it's the browsers responsibility to
<span class="function"><a href="function.urlencode.php" class="function">urlencode()</a></span> 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.
</span>
</p></blockquote>
<div class="example" id="example-5464">
<p><strong>Example #2 Data to be edited by the user</strong></p>
<div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB"><?php<br /> </span><span style="color: #007700">echo </span><span style="color: #DD0000">"<textarea name='mydata'>\n"</span><span style="color: #007700">;<br /> echo </span><span style="color: #0000BB">htmlspecialchars</span><span style="color: #007700">(</span><span style="color: #0000BB">$data</span><span style="color: #007700">).</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /> echo </span><span style="color: #DD0000">"</textarea>"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?></span>
</span>
</code></div>
</div>
</div>
<blockquote class="note"><p><strong class="note">Note</strong>:
<span class="simpara">
The data is shown in the browser as intended, because the browser will
interpret the HTML escaped symbols.
</span>
<span class="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,
everything is handled automagically.
</span>
</p></blockquote>
<div class="example" id="example-5465">
<p><strong>Example #3 In a URL</strong></p>
<div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB"><?php<br /> </span><span style="color: #007700">echo </span><span style="color: #DD0000">'<a href="' </span><span style="color: #007700">. </span><span style="color: #0000BB">htmlspecialchars</span><span style="color: #007700">(</span><span style="color: #DD0000">"/nextpage.php?stage=23&data=" </span><span style="color: #007700">.<br /> </span><span style="color: #0000BB">urlencode</span><span style="color: #007700">(</span><span style="color: #0000BB">$data</span><span style="color: #007700">)) . </span><span style="color: #DD0000">'">'</span><span style="color: #007700">.</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?></span>
</span>
</code></div>
</div>
</div>
<blockquote class="note"><p><strong class="note">Note</strong>:
<span class="simpara">
In fact you are faking a HTML GET request, therefore it's necessary to
manually <span class="function"><a href="function.urlencode.php" class="function">urlencode()</a></span> the data.
</span>
</p></blockquote>
<blockquote class="note"><p><strong class="note">Note</strong>:
<span class="simpara">
You need to <span class="function"><a href="function.htmlspecialchars.php" class="function">htmlspecialchars()</a></span> the whole URL, because the
URL occurs as value of an HTML-attribute. In this case, the browser
will first un- <span class="function"><a href="function.htmlspecialchars.php" class="function">htmlspecialchars()</a></span> the value, and then pass
the URL on. PHP will understand the URL correctly, because you
<span class="function"><a href="function.urlencode.php" class="function">urlencode()</a></span>d the data.
</span>
<span class="simpara">
You'll notice that the <em>&</em> in the URL is replaced
by <em>&amp;</em>. Although most browsers will recover
if you forget this, this isn't always possible. So even if your URL is
not dynamic, you <em class="emphasis">need</em> to
<span class="function"><a href="function.htmlspecialchars.php" class="function">htmlspecialchars()</a></span> the URL.
</span>
</p></blockquote>
</p>
</dd>
</dl>
<dl class="qandaentry" id="faq.html.form-image">
<dt><strong>
I'm trying to use an <input type="image"> tag, but
the <var class="varname"><var class="varname">$foo.x</var></var> and <var class="varname"><var class="varname">$foo.y</var></var> variables
aren't available. <var class="varname"><var class="varname"><a href="reserved.variables.get.php" class="classname">$_GET['foo.x']</a></var></var> isn't existing
either. Where are they?
</strong></dt>
<dd class="answer">
<p class="para">
When submitting a form, it is possible to use an image instead of
the standard submit button with a tag like:
<div class="example-contents">
<div class="htmlcode"><pre class="htmlcode"><input type="image" src="image.gif" name="foo" /></pre>
</div>
</div>
When the user clicks somewhere on the image, the accompanying form
will be transmitted to the server with two additional variables:
<var class="varname"><var class="varname">foo.x</var></var> and <var class="varname"><var class="varname">foo.y</var></var>.
</p>
<p class="para">
Because <var class="varname"><var class="varname">foo.x</var></var> and <var class="varname"><var class="varname">foo.y</var></var> would
make invalid variable names in PHP, they are automagically converted to
<var class="varname"><var class="varname">foo_x</var></var> and <var class="varname"><var class="varname">foo_y</var></var>. That is, the
periods are replaced with underscores. So, you'd access these variables
like any other described within the section on retrieving
<a href="language.variables.external.php" class="link">variables from external
sources</a>. For example, <var class="varname"><var class="varname"><a href="reserved.variables.get.php" class="classname">$_GET['foo_x']</a></var></var>.
<blockquote class="note"><p><strong class="note">Note</strong>:
<p class="para">
Spaces in request variable names are converted to underscores.
</p>
</p></blockquote>
</p>
</dd>
</dl>
<dl class="qandaentry" id="faq.html.arrays">
<dt><strong>
How do I create arrays in a HTML <form>?
</strong></dt>
<dd class="answer">
<p class="para">
To get your <form> result sent as an
<a href="language.types.array.php" class="link">array</a> to your PHP script
you name the <input>, <select> or <textarea>
elements like this:
<div class="example-contents">
<div class="htmlcode"><pre class="htmlcode"><input name="MyArray[]" />
<input name="MyArray[]" />
<input name="MyArray[]" />
<input name="MyArray[]" /></pre>
</div>
</div>
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:
<div class="example-contents">
<div class="htmlcode"><pre class="htmlcode"><input name="MyArray[]" />
<input name="MyArray[]" />
<input name="MyOtherArray[]" />
<input name="MyOtherArray[]" /></pre>
</div>
</div>
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:
<div class="example-contents">
<div class="htmlcode"><pre class="htmlcode"><input name="AnotherArray[]" />
<input name="AnotherArray[]" />
<input name="AnotherArray[email]" />
<input name="AnotherArray[phone]" /></pre>
</div>
</div>
The AnotherArray array will now contain the keys 0, 1, email and phone.
</p>
<p class="para">
<blockquote class="note"><p><strong class="note">Note</strong>:
<p class="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.
</p>
</p></blockquote>
</p>
<p class="para">
See also
<a href="ref.array.php" class="link">Array Functions</a> and
<a href="language.variables.external.php" class="link">Variables From External
Sources</a>.
</p>
</dd>
</dl>
<dl class="qandaentry" id="faq.html.select-multiple">
<dt><strong>
How do I get all the results from a select multiple HTML tag?
</strong></dt>
<dd class="answer">
<p class="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. I.e.
<div class="example-contents">
<div class="htmlcode"><pre class="htmlcode"><select name="var" multiple="yes"></pre>
</div>
</div>
Each selected option will arrive at the action handler as:
<div class="example-contents"><div class="cdata"><pre>
var=option1
var=option2
var=option3
</pre></div></div>
Each option will overwrite the contents of the previous
<var class="varname"><var class="varname">$var</var></var> variable. The solution is to use
PHP's "array from form element" feature. The following
should be used:
<div class="example-contents">
<div class="htmlcode"><pre class="htmlcode"><select name="var[]" multiple="yes"></pre>
</div>
</div>
This tells PHP to treat <var class="varname"><var class="varname">$var</var></var> as an array and
each assignment of a value to var[] adds an item to the array.
The first item becomes <var class="varname"><var class="varname">$var[0]</var></var>, the next
<var class="varname"><var class="varname">$var[1]</var></var>, etc. The <span class="function"><a href="function.count.php" class="function">count()</a></span>
function can be used to determine how many options were selected,
and the <span class="function"><a href="function.sort.php" class="function">sort()</a></span> function can be used to sort
the option array if necessary.
</p>
<p class="para">
Note that if you are using JavaScript the <em>[]</em>
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:
<div class="example-contents"><div class="cdata"><pre>
variable = documents.forms[0].elements['var[]'];
</pre></div></div>
</p>
</dd>
</dl>
<dl class="qandaentry" id="faq.html.javascript-variable">
<dt><strong>
How can I pass a variable from Javascript to PHP?
</strong></dt>
<dd class="answer">
<p class="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.
</p>
<p class="para">
It is, however, possible to pass variables between the two.
One way of accomplishing this is to generate Javascript code
with PHP, and have the browser refresh itself, passing specific
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.
</p>
<p class="para">
<div class="example" id="example-5466">
<p><strong>Example #4 Generating Javascript with PHP</strong></p>
<div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB"><?php<br /></span><span style="color: #007700">if (isset(</span><span style="color: #0000BB">$_GET</span><span style="color: #007700">[</span><span style="color: #DD0000">'width'</span><span style="color: #007700">]) AND isset(</span><span style="color: #0000BB">$_GET</span><span style="color: #007700">[</span><span style="color: #DD0000">'height'</span><span style="color: #007700">])) {<br /> </span><span style="color: #FF8000">// output the geometry variables<br /> </span><span style="color: #007700">echo </span><span style="color: #DD0000">"Screen width is: "</span><span style="color: #007700">. </span><span style="color: #0000BB">$_GET</span><span style="color: #007700">[</span><span style="color: #DD0000">'width'</span><span style="color: #007700">] .</span><span style="color: #DD0000">"<br />\n"</span><span style="color: #007700">;<br /> echo </span><span style="color: #DD0000">"Screen height is: "</span><span style="color: #007700">. </span><span style="color: #0000BB">$_GET</span><span style="color: #007700">[</span><span style="color: #DD0000">'height'</span><span style="color: #007700">] .</span><span style="color: #DD0000">"<br />\n"</span><span style="color: #007700">;<br />} else {<br /> </span><span style="color: #FF8000">// pass the geometry variables<br /> // (preserve the original query string<br /> // -- post variables will need to handled differently)<br /><br /> </span><span style="color: #007700">echo </span><span style="color: #DD0000">"<script language='javascript'>\n"</span><span style="color: #007700">;<br /> echo </span><span style="color: #DD0000">" location.href=\"</span><span style="color: #007700">${</span><span style="color: #0000BB">_SERVER</span><span style="color: #007700">[</span><span style="color: #DD0000">'SCRIPT_NAME'</span><span style="color: #007700">]}</span><span style="color: #DD0000">?</span><span style="color: #007700">${</span><span style="color: #0000BB">_SERVER</span><span style="color: #007700">[</span><span style="color: #DD0000">'QUERY_STRING'</span><span style="color: #007700">]}</span><span style="color: #DD0000">"<br /> </span><span style="color: #007700">. </span><span style="color: #DD0000">"&width=\" + screen.width + \"&height=\" + screen.height;\n"</span><span style="color: #007700">;<br /> echo </span><span style="color: #DD0000">"</script>\n"</span><span style="color: #007700">;<br /> exit();<br />}<br /></span><span style="color: #0000BB">?></span>
</span>
</code></div>
</div>
</div>
</p>
</dd>
</dl>
</div>
<?php manual_footer(); ?>