1
0
mirror of https://github.com/php/doc-zh.git synced 2026-03-23 22:52:08 +01:00
Files
archived-doc-zh/faq/html.xml
Louis-Arnaud ecf1e5b1cc Check translation for faq (#1016)
* Fix faq: critical translation errors and Chinese typos

- using.xml: 区分大小写→不区分大小写 (opposite meaning), update shorthand notation usage
- mailinglist.xml: remove stray 'å' character, <literal>→<emphasis> for emphasis
- com.xml: fix &amp;true; → &true; entity references, <xref>→<link> to match EN
- obtaining.xml: 组和→组合 typo
- passwords.xml: 将会窃取→将被窃取 (missing passive voice)
- html.xml: add missing <example><title> wrapper for Javascript example

* Fix faq/installation.xml: sync with EN, remove outdated content

- Remove obsolete MacOS parenthetical
- Remove extra Windows threading paragraph
- php.ini-dist → php.ini-development
- ereg → preg_match (ereg removed in PHP 7)
- php4 → php5 in paths, php4ts.dll → php5ts.dll
- Add missing php_ini_loaded_file reference
- Remove Win98/Me autoexec.bat section
- Remove obsolete Windows system directory paragraph
- Simplify Windows PATH instructions to match EN
- Remove PHP 4 handler from multiviews example

* Fix faq/com.xml: replace Unicode smart quotes with ASCII quotes

The linkend attributes used Unicode right double quotation marks (U+201D)
instead of ASCII double quotes, causing XML parse failures in CI.

* Update com.xml

将中文句式中的英文引号修改为中文引号

---------

Co-authored-by: mowangjuanzi <mowangjuanzi@petalmail.com>
2026-03-17 18:02:00 +08:00

258 lines
11 KiB
XML
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<!-- EN-Revision: 9e6c3416c5c285f807a734e4663c399612777d7e Maintainer: mowangjuanzi Status: ready -->
<!-- CREDITS: Luffy -->
<chapter xml:id="faq.html" xmlns="http://docbook.org/ns/docbook">
<title>PHP 和 HTML</title>
<titleabbrev>PHP 和 HTML</titleabbrev>
<para>
PHP 和 HTML 有很多相互作用PHP 能生成 HTMLHTML 可以向 PHP
传递信息。在阅读这些常见问题之前,先学会怎样<link linkend="language.variables.external">
PHP 之外取得变量</link>很重要。此主题的手册页也包括很多例子。
</para>
<qandaset>
<qandaentry xml:id="faq.html.encoding">
<question>
<para>当我通过表单URL 传值时需要用什么编码/解码方法?</para>
</question>
<answer>
<para>在几个环节上编码方式很重要。假定有
<type>string</type>
<varname>$data</varname>,其中包含了想通过非编码方式传递的字符串,那这是相关步骤:
<itemizedlist>
<listitem>
<para>HTML 解析。要指定一个任意的字符串,
<emphasis>必须</emphasis>将其放在双引号中,并用
<function>htmlspecialchars</function>处理整个值。</para>
</listitem>
<listitem>
<para>URLURL 由几部分组成。如果希望自己的数据被当作其中一项来解释,
<emphasis>必须</emphasis><function>urlencode</function> 对其编码。
</para>
</listitem>
</itemizedlist>
</para>
<para>
<example>
<title>隐藏的 HTML 表单单元</title>
<programlisting role="php">
<![CDATA[
<?php
echo '<input type="hidden" value="' . htmlspecialchars($data) . '" />'."\n";
?>
]]>
</programlisting>
</example>
<note>
<simpara>
<function>urlencode</function>来处理
<varname>$data</varname>是错误的,因为是浏览器的责任来
<function>urlencode</function>数据。所有流行的浏览器都能正确处理。注意不论何种方法(例如 GET 或 POST都会这样。不过只会在用 GET 请求时注意到这一点,因为 POST 请求通常是隐藏的。</simpara>
</note>
<example>
<title>等待用户编辑的数据</title>
<programlisting role="php">
<![CDATA[
<?php
echo "<textarea name='mydata'>\n";
echo htmlspecialchars($data)."\n";
echo "</textarea>";
?>
]]>
</programlisting>
</example>
<note>
<simpara>数据会按照预期的显示在浏览器中,因为浏览器会解释 HTML 转义符号。</simpara>
<simpara>当提交时,不论是 GET 或者 POST 方法,数据都会被浏览器进行 urlencode 来传输,并直接被 PHP urldecode。所以最终不需要自己处理任何 urlencoding/urldecoding全都是自动处理的。</simpara>
</note>
<example>
<title>URL 中的例子</title>
<programlisting role="php">
<![CDATA[
<?php
echo '<a href="' . htmlspecialchars("/nextpage.php?stage=23&data=" .
urlencode($data)) . '">'."\n";
?>
]]>
</programlisting>
</example>
<note>
<simpara>事实上这在编造一个 HTML 的 GET 请求,因此需要手工对数据进行
<function>urlencode</function></simpara>
</note>
<note>
<simpara>需要对整个 URL 进行
<function>htmlspecialchars</function>,因为 URL 是作为 HTML 属性的一个值出现的。在本例中,浏览器会首先对值进行 un-
<function>htmlspecialchars</function>,然后再传递此 URL。PHP 将能正确理解 URL因为对数据进行了
<function>urlencode</function></simpara>
<simpara>要注意到 URL 中的
<literal>&amp;</literal>被替换成了
<literal>&amp;amp;</literal>。如果忘了这一步,尽管大多数浏览器都能恢复,但也不总是这样。因此即使 URL 不是动态的,也
<emphasis>需要</emphasis>对 URL 进行
<function>htmlspecialchars</function></simpara>
</note>
</para>
<!-- TODO: a note about addgpcslashes? -->
</answer>
</qandaentry>
<qandaentry xml:id="faq.html.form-image">
<question>
<para>我在试用 &lt;input type=&quot;image&quot;&gt; 标记,但是没有
<varname>$foo.x</varname>
<varname>$foo.y</varname>变量,它们哪去了?</para>
</question>
<answer>
<para>当提交表单时,可以用图片代替标准的提交按钮,用类似这样的标记:
<programlisting role="html">
<![CDATA[
<input type="image" src="image.gif" name="foo" />
]]>
</programlisting>当用户点击了图片的任何部分,该表单会被发送到服务器并加上两个额外的变量:
<varname>foo.x</varname>
<varname>foo.y</varname></para>
<para>因为
<varname>foo.x</varname>
<varname>foo.y</varname>在 PHP 中会成为非法的变量名,它们被自动转换成了
<varname>foo_x</varname>
<varname>foo_y</varname>。也就是用下划线代替了点。因此,可以按照在
<link linkend="language.variables.external">来自 PHP 之外的变量</link>这一节中说明的那样访问这些变量。例如,
<varname>$_GET[&#39;foo_x&#39;]</varname>
<note>
<para>请求变量名中的空格被转换为下划线。</para>
</note></para>
</answer>
</qandaentry>
<qandaentry xml:id="faq.html.arrays">
<question>
<para>怎样在 HTML 的 &lt;form&gt; 中建立数组?</para>
</question>
<answer>
<para>要使你的 &lt;form&gt; 结果被当成
<link linkend="language.types.array">array</link>发送到 PHP 脚本,要对 &lt;input&gt;&lt;select&gt; 或者 &lt;textarea&gt; 单元这样命名:
<programlisting role="html">
<![CDATA[
<input name="MyArray[]" />
<input name="MyArray[]" />
<input name="MyArray[]" />
<input name="MyArray[]" />
]]>
</programlisting>注意变量名后的方括号,这使其成为一个数组。可以通过给不同的单元分配相同的名字来把单元分组到不同的数组里:
<programlisting role="html">
<![CDATA[
<input name="MyArray[]" />
<input name="MyArray[]" />
<input name="MyOtherArray[]" />
<input name="MyOtherArray[]" />
]]>
</programlisting>这将产生两个数组MyArray 和 MyOtherArray并发送给 PHP 脚本。还可以给数组分配指定的键名:
<programlisting role="html">
<![CDATA[
<input name="AnotherArray[]" />
<input name="AnotherArray[]" />
<input name="AnotherArray[email]" />
<input name="AnotherArray[phone]" />
]]>
</programlisting>AnotherArray 数组将包含键名 01email 和 phone。</para>
<para>
<note>
<para>指定数组的键名是 HTML 的可选项。如果不指定键名,则数组被按照单元在表单中出现的顺序填充。第一个例子将包含键名 012 和 3。</para>
</note>
</para>
<para>参见
<link linkend="ref.array">数组函数</link>
<link linkend="language.variables.external">来自 PHP 之外的变量</link></para>
</answer>
</qandaentry>
<qandaentry xml:id="faq.html.select-multiple">
<question>
<para>怎样从可多选的 HTML 的 select multiple 标记中得到所有结果?</para>
</question>
<answer>
<para>可多选的 select multiple 标记是 HTML 的一个构造,允许用户从一个列表中选择多个项目。这些项目接着被传递给该表单 action 中指定的处理程序。问题是它们都会被用同样的名字传递。例如:
<programlisting role="html">
<![CDATA[
<select name="var" multiple="yes">
]]>
</programlisting>每个被选项将这样被传递到表单处理程序:
<programlisting>var=option1 var=option2 var=option3</programlisting>每个选项将覆盖前面一个
<varname>$var</varname>变量的内容。解决方案是用 PHP 的“表单单元数组”特性。使用方法如下:
<programlisting role="html">
<![CDATA[
<select name="var[]" multiple="yes">
]]>
</programlisting>这将告诉 PHP 将
<varname>$var</varname>当成数组对待,每个对 var[] 的赋值都会给数组增加一项。第一项将成为
<varname>$var[0]</varname>,下一个是
<varname>$var[1]</varname>,等等。可以用
<function>count</function>函数来测定选择了多少个项目,必要时可以用
<function>sort</function>函数来对选项的数组进行排序。</para>
<para>注意如果在 JavaScript 中通过名字来引用单元,单元名字中的
<literal>[]</literal>可能会造成问题。用表单单元中的数字序号来替代,或者将变量名用单引号括起来并用其作为单元数组的索引,例如:
<programlisting>
variable = document.forms[0].elements['var[]'];
</programlisting>
</para>
</answer>
</qandaentry>
<qandaentry xml:id="faq.html.javascript-variable">
<question>
<para>怎样从 Javascript 传递一个变量到 PHP</para>
</question>
<answer>
<para>由于 Javascript (通常情况下)是客户端技术,而 PHP (通常情况下)是服务器端技术,而且 HTTP 是一种“无状态”协议,因此两种语言之间不能直接共享变量。</para>
<para>但是,有可能在二者之间传递变量。一种实现的方法是用 PHP 生成 Javascript 代码,并让浏览器自动刷新,将特定的变量传递回 PHP 脚本。以下例子显示了如何这样做——让 PHP 代码取得显示屏幕的高度和宽度,通常只能在客户端这么做。</para>
<para>
<example>
<title>用 PHP 生成 Javascript</title>
<programlisting role="php">
<![CDATA[
<?php
if (isset($_GET['width']) AND isset($_GET['height'])) {
// output the geometry variables
echo "Screen width is: ". $_GET['width'] ."<br />\n";
echo "Screen height is: ". $_GET['height'] ."<br />\n";
} else {
// pass the geometry variables
// (preserve the original query string
// -- post variables will need to handled differently)
echo "<script language='javascript'>\n";
echo " location.href=\"{$_SERVER['SCRIPT_NAME']}?{$_SERVER['QUERY_STRING']}"
. "&width=\" + screen.width + \"&height=\" + screen.height;\n";
echo "</script>\n";
exit();
}
?>
]]>
</programlisting>
</example>
</para>
</answer>
</qandaentry>
</qandaset>
</chapter>
<!-- 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
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/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
-->