📄 faq.html.html
字号:
Because <var class="varname">foo.x</var> and <var class="varname">foo.y</var> would make invalid variable names in PHP, they are automagically converted to <var class="varname">foo_x</var> and <var class="varname">foo_y</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.html" class="link">variables from external sources</a>. For example, <var class="varname"><a href="reserved.variables.get.html" class="classname">$_GET['foo_x']</a></var>. <blockquote><p><b class="note">Note</b>: Spaces in request variable names are converted to underscores. <br /> </p></blockquote> </p> </dd> </dl> <dl> <dt><strong> <p class="para">How do I create arrays in a HTML <form>?</p> </strong></dt> <dd><a name="faq.html.arrays"></a> <p class="para"> To get your <form> result sent as an <a href="language.types.array.html" class="link">array</a> to your PHP script you name the <input>, <select> or <textarea> elements like this: <div class="example-contents"><div class="cdata"><pre><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="cdata"><pre><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="cdata"><pre><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><p><b class="note">Note</b>: 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. <br /> </p></blockquote> </p> <p class="para"> See also <a href="ref.array.html" class="link">Array Functions</a> and <a href="language.variables.external.html" class="link">Variables From External Sources</a>. </p> </dd> </dl> <dl> <dt><strong> <p class="para"> How do I get all the results from a select multiple HTML tag? </p> </strong></dt> <dd><a name="faq.html.select-multiple"></a> <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="cdata"><pre><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=option1var=option2var=option3 </pre></div></div> Each option will overwrite the contents of the previous <var class="varname">$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="cdata"><pre><select name="var[]" multiple="yes"></pre></div> </div> This tells PHP to treat <var class="varname">$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[0]</var>, the next <var class="varname">$var[1]</var>, etc. The <a href="function.count.html" class="function">count()</a> function can be used to determine how many options were selected, and the <a href="function.sort.html" class="function">sort()</a> function can be used to sort the option array if necessary. </p> <p class="para"> Note that if you are using JavaScript the <i>[]</i> 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> <dt><strong> <p class="para"> How can I pass a variable from Javascript to PHP? </p> </strong></dt> <dd><a name="faq.html.javascript-variable"></a> <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-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=\"${_SERVER['SCRIPT_NAME']}?${_SERVER['QUERY_STRING']}"<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> </p> </dd> </dl> </div> </div><hr /><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></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -