📄 faq.html.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <title>PHP and HTML</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="faq.using.html">Using PHP</a></div> <div class="next" style="text-align: right; float: right;"><a href="faq.com.html">PHP and COM</a></div> <div class="up"><a href="faq.html">FAQ</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div><hr /><div> <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.html" class="link"> variables from external sources</a>. The manual page on this topic includes many examples as well. Pay close attention to what <i>register_globals</i> 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> <dl> <dt><strong> <p class="para"> What encoding/decoding do I need when I pass a value through a form/URL? </p> </strong></dt> <dd><a name="faq.html.encoding"></a> <p class="para"> There are several stages for which encoding is important. Assuming that you have a <a href="language.types.string.html" class="type string">string</a> <var class="varname">$data</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 <a href="function.htmlspecialchars.html" class="function">htmlspecialchars()</a> 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 <a href="function.urlencode.html" class="function">urlencode()</a>. </p> </li> </ul> </p> <p class="para"> <div class="example"> <p><b>Example #1 A hidden HTML form element</b></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">"' />\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> <blockquote><p><b class="note">Note</b>: <span class="simpara"> It is wrong to <a href="function.urlencode.html" class="function">urlencode()</a> <var class="varname">$data</var>, because it's the browsers responsibility to <a href="function.urlencode.html" class="function">urlencode()</a> 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"> <p><b>Example #2 Data to be edited by the user</b></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><p><b class="note">Note</b>: <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"> <p><b>Example #3 In a URL</b></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">"'>\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> <blockquote><p><b class="note">Note</b>: <span class="simpara"> In fact you are faking a HTML GET request, therefore it's necessary to manually <a href="function.urlencode.html" class="function">urlencode()</a> the data. </span> </p></blockquote> <blockquote><p><b class="note">Note</b>: <span class="simpara"> You need to <a href="function.htmlspecialchars.html" class="function">htmlspecialchars()</a> the whole URL, because the URL occurs as value of an HTML-attribute. In this case, the browser will first un-<a href="function.htmlspecialchars.html" class="function">htmlspecialchars()</a> the value, and then pass the URL on. PHP will understand the URL correctly, because you <b>urlencoded()</b> the data. </span> <span class="simpara"> You'll notice that the <i>&</i> in the URL is replaced by <i>&amp;</i>. 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 <a href="function.htmlspecialchars.html" class="function">htmlspecialchars()</a> the URL. </span> </p></blockquote> </p> </dd> </dl> <dl> <dt><strong> <p class="para"> I'm trying to use an <input type="image"> tag, but the <var class="varname">$foo.x</var> and <var class="varname">$foo.y</var> variables aren't available. <var class="varname"><a href="reserved.variables.get.html" class="classname">$_GET['foo.x']</a></var> isn't existing either. Where are they? </p> </strong></dt> <dd><a name="faq.html.form-image"></a> <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="cdata"><pre><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">foo.x</var> and <var class="varname">foo.y</var>. </p> <p class="para">
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -