📄 language.variables.external.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <title>Variables From External Sources</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="language.variables.html">Variables</a></div> <div class="next" style="text-align: right; float: right;"><a href="language.constants.html">Constants</a></div> <div class="up"><a href="language.variables.html">Variables</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div><hr /><div id="language.variables.external" class="sect1"> <h2 class="title">Variables From External Sources</h2> <div id="language.variables.external.form" class="sect2"> <h3 class="title">HTML Forms (GET and POST)</h3> <p class="simpara"> When a form is submitted to a PHP script, the information from that form is automatically made available to the script. There are many ways to access this information, for example: </p> <p class="para"> <div class="example"> <p><b>Example #1 A simple HTML form</b></p> <div class="example-contents"><div class="cdata"><pre><form action="foo.php" method="post"> Name: <input type="text" name="username" /><br /> Email: <input type="text" name="email" /><br /> <input type="submit" name="submit" value="Submit me!" /></form></pre></div> </div> </div> </p> <p class="para"> Depending on your particular setup and personal preferences, there are many ways to access data from your HTML forms. Some examples are: </p> <p class="para"> <div class="example"> <p><b>Example #2 Accessing data from a simple POST HTML form</b></p> <div class="example-contents"><div class="cdata"><pre><?php // Available since PHP 4.1.0 echo $_POST['username']; echo $_REQUEST['username']; import_request_variables('p', 'p_'); echo $p_username;// Unavailable since PHP 6. As of PHP 5.0.0, these long predefined// variables can be disabled with the register_long_arrays directive. echo $HTTP_POST_VARS['username'];// Available if the PHP directive register_globals = on. As of // PHP 4.2.0 the default value of register_globals = off.// Using/relying on this method is not preferred. echo $username;?></pre></div> </div> </div> </p> <p class="para"> Using a GET form is similar except you'll use the appropriate GET predefined variable instead. GET also applies to the QUERY_STRING (the information after the '?' in a URL). So, for example, <i>http://www.example.com/test.php?id=3</i> contains GET data which is accessible with <var class="varname"><a href="reserved.variables.get.html" class="classname">$_GET['id']</a></var>. See also <var class="varname"><a href="reserved.variables.request.html" class="classname">$_REQUEST</a></var> and <a href="function.import-request-variables.html" class="function">import_request_variables()</a>. </p> <blockquote><p><b class="note">Note</b>: <a href="language.variables.superglobals.html" class="link">Superglobal arrays</a>, like <var class="varname"><a href="reserved.variables.post.html" class="classname">$_POST</a></var> and <var class="varname"><a href="reserved.variables.get.html" class="classname">$_GET</a></var>, became available in PHP 4.1.0 <br /> </p></blockquote> <p class="para"> As shown, before PHP 4.2.0 the default value for <a href="ini.core.html#ini.register-globals" class="link">register_globals</a> was <em class="emphasis">on</em>. The PHP community is encouraging all to not rely on this directive as it's preferred to assume it's <em class="emphasis">off</em> and code accordingly. </p> <blockquote><p><b class="note">Note</b>: The <a href="info.configuration.html#ini.magic-quotes-gpc" class="link">magic_quotes_gpc</a> configuration directive affects Get, Post and Cookie values. If turned on, value (It's "PHP!") will automagically become (It\'s \"PHP!\"). Escaping is needed for DB insertion. See also <a href="function.addslashes.html" class="function">addslashes()</a>, <a href="function.stripslashes.html" class="function">stripslashes()</a> and <a href="sybase.configuration.html#ini.magic-quotes-sybase" class="link">magic_quotes_sybase</a>. <br /> </p></blockquote> <p class="simpara"> PHP also understands arrays in the context of form variables (see the <a href="faq.html.html" class="link">related faq</a>). You may, for example, group related variables together, or use this feature to retrieve values from a multiple select input. For example, let's post a form to itself and upon submission display the data: </p> <p class="para"> <div class="example"> <p><b>Example #3 More complex form variables</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">if (</span><span style="color: #0000BB">$_POST</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">'<pre>'</span><span style="color: #007700">;<br /> echo </span><span style="color: #0000BB">htmlspecialchars</span><span style="color: #007700">(</span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$_POST</span><span style="color: #007700">, </span><span style="color: #0000BB">true</span><span style="color: #007700">));<br /> echo </span><span style="color: #DD0000">'</pre>'</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?><br /></span><form action="" method="post"><br /> Name: <input type="text" name="personal[name]" /><br /><br /> Email: <input type="text" name="personal[email]" /><br /><br /> Beer: <br /><br /> <select multiple name="beer[]"><br /> <option value="warthog">Warthog</option><br /> <option value="guinness">Guinness</option><br /> <option value="stuttgarter">Stuttgarter Schwabenbr盲u</option><br /> </select><br /><br /> <input type="submit" value="submit me!" /><br /></form></span></code></div> </div> </div> </p> <div id="language.variables.external.form.submit" class="sect3"> <h4 class="title">IMAGE SUBMIT variable names</h4> <p class="simpara"> When submitting a form, it is possible to use an image instead of the standard submit button with a tag like:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -