結(jié)合PHP使用HTML表單(2)
Jun 21, 2016 am 09:15 AM清單 2 中的表示法當(dāng)然是方便的,但它僅在 PHP 偽指令 register_globals 被設(shè)置為 on 時(shí)才可用。在版本 4.2 以前,這是缺省設(shè)置,許多 PHP 開(kāi)發(fā)人員甚至沒(méi)有意識(shí)到有這樣的問(wèn)題。然而,從版本 4.2 開(kāi)始,register_globals 的缺省設(shè)置是 off,在此情況下,該表示法不能正常工作,因?yàn)椴辉儆眠m當(dāng)?shù)闹祫?chuàng)建并初始化變量。
然而,您可以用其它的方法初始化這些變量。第一個(gè)方法是更改 register_globals 的值。許多使用共享服務(wù)器的開(kāi)發(fā)人員無(wú)權(quán)為整個(gè)服務(wù)器更改該值,但可以針對(duì)某個(gè)特定站點(diǎn)改變行為。如您有 .htaccess 文件的訪(fǎng)問(wèn)權(quán),您可以通過(guò)添加以下偽指令啟用 register_globals:
php_flag register_globals on
鑒于對(duì)該特性是否可用的不確定性,建議開(kāi)發(fā)人員最好不要使用或依靠這種獲取變量的方法。那么您有什么選擇呢?
如果您的系統(tǒng)運(yùn)行的是版本 4.1 或更高版本,則您的另一個(gè)選擇是使用 import_request_variables() 有選擇地注冊(cè)全局變量集合。您可以使用該函數(shù)導(dǎo)入 get、post 和 cookie 值,而且如果您愿意的話(huà),還可以給每項(xiàng)添加前綴。例如:
import_request_variables(gp, "formval_");
echo "Ship = ".$formval_ship;
echo "
";
echo "Tripdate = ".$formval_tripdate;
echo "
";
echo "Exploration = ".$formval_exploration;
echo "
";
echo "Contact = ".$formval_contact;
?>
這里,導(dǎo)入了 get 和 post 值 — 使用 c 來(lái)導(dǎo)入 cookie 值 — 而且由于 p 跟在 g 之后,所以 post 值將覆蓋同名的 get 值。
但如果您象許多開(kāi)發(fā)人員一樣沒(méi)有運(yùn)行版本 4.1 或更高版本,那怎么辦呢?
訪(fǎng)問(wèn)表單值集合
對(duì)于那些運(yùn)行較早版本或不愿使用全局變量的人來(lái)說(shuō),可以選擇使用 $HTTP_GET_VARS 和 $HTTP_POST_VARS 數(shù)組。盡管并不贊成使用這些集合,但它們?nèi)匀豢捎茫胰员粡V泛使用。當(dāng)真的不再使用它們時(shí),將用版本 4.1 中添加的 $_GET 和 $_POST 數(shù)組替代它們。
這兩類(lèi)數(shù)組的類(lèi)型都是散列表(hash table)。散列表是通過(guò)字符串值而不是整數(shù)來(lái)建立索引的數(shù)組。使用表單時(shí),可以通過(guò)值的名稱(chēng)來(lái)訪(fǎng)問(wèn)值,如清單 3 所示:
清單 3. 通過(guò)散列表訪(fǎng)問(wèn)表單值
$ship_value = $HTTP_GET_VARS['ship'];
echo $ship_value;
echo "
";
$tripdate_value = $HTTP_GET_VARS['tripdate'];
echo $tripdate_value;
echo "
";
$exploration_value= $HTTP_GET_VARS['exploration'];
echo $exploration_value;
echo "
";
$contact_value = $HTTP_GET_VARS['contact'];
echo $contact_value;
?>
使用該方法,您可以通過(guò)名稱(chēng)來(lái)檢索每個(gè)字段的值。
單名,多值
到現(xiàn)在為止,每個(gè)名稱(chēng)僅對(duì)應(yīng)一個(gè)值。如果有多個(gè)值會(huì)怎樣?例如,crew species 列表框允許用名稱(chēng) crew 提交多個(gè)值。
理想情況下,您希望將這些值作為數(shù)組使用,這樣就可以顯式地檢索它們。要實(shí)現(xiàn)這一點(diǎn),您必須對(duì) HTML 頁(yè)面稍加改動(dòng)。要作為數(shù)組提交的字段應(yīng)該用方括號(hào)命名,如 crew[] 中:
清單 4. 修改 HTML 頁(yè)面
...
...
一旦您作出更改,檢索表單值實(shí)際上產(chǎn)生數(shù)組:
清單 5. 將變量作為數(shù)組訪(fǎng)問(wèn)
...
$crew_values = $HTTP_GET_VARS['crew'];
echo "0) ".$crew_values[0];
echo "
";
echo "1) ".$crew_values[1];
echo "
";
echo "2) ".$crew_values[2];
...
現(xiàn)在,提交頁(yè)面后會(huì)顯示多個(gè)值:
0) snertal
1) gosny
2)
首先要注意這是一個(gè)下標(biāo)從 0 開(kāi)始的數(shù)組。第一個(gè)遇到的值在位置 0 中,接下來(lái)的值在位置 1,以此類(lèi)推。在本例中,我只提交了兩個(gè)值,所以第三項(xiàng)為空。
通常,您不知道將提交多少項(xiàng),因此您可以利用它是數(shù)組這一事實(shí)使用 sizeof() 函數(shù)來(lái)確定提交了多少值,而不必直接調(diào)用每一項(xiàng):
清單 6. 確定數(shù)組的大小
...
for ($i = 0; $i
echo $crew_values[$i];
echo "
";
}
...
然而,有時(shí)問(wèn)題不是值太多,而是根本沒(méi)有

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

The most popular Go frameworks at present are: Gin: lightweight, high-performance web framework, simple and easy to use. Echo: A fast, highly customizable web framework that provides high-performance routing and middleware. GorillaMux: A fast and flexible multiplexer that provides advanced routing configuration options. Fiber: A performance-optimized, high-performance web framework that handles high concurrent requests. Martini: A modular web framework with object-oriented design that provides a rich feature set.

Laravel is a popular PHP framework that is highly scalable and efficient. It provides many powerful tools and libraries that allow developers to quickly build high-quality web applications. Among them, LaravelEcho and Pusher are two very important tools through which WebSockets communication can be easily implemented. This article will detail how to use these two tools in Laravel applications. What are WebSockets? WebSockets

In today's era of rapid technological development, programming languages ??are springing up like mushrooms after a rain. One of the languages ??that has attracted much attention is the Go language, which is loved by many developers for its simplicity, efficiency, concurrency safety and other features. The Go language is known for its strong ecosystem with many excellent open source projects. This article will introduce five selected Go language open source projects and lead readers to explore the world of Go language open source projects. KubernetesKubernetes is an open source container orchestration engine for automated

"Go Language Development Essentials: 5 Popular Framework Recommendations" As a fast and efficient programming language, Go language is favored by more and more developers. In order to improve development efficiency and optimize code structure, many developers choose to use frameworks to quickly build applications. In the world of Go language, there are many excellent frameworks to choose from. This article will introduce 5 popular Go language frameworks and provide specific code examples to help readers better understand and use these frameworks. 1.GinGin is a lightweight web framework with fast

Detailed explanation of the role and usage of the echo keyword in PHP PHP is a widely used server-side scripting language, which is widely used in web development. The echo keyword is a method used to output content in PHP. This article will introduce in detail the function and use of the echo keyword. Function: The main function of the echo keyword is to output content to the browser. In web development, we need to dynamically present data to the front-end page. At this time, we can use the echo keyword to output the data to the page. e

With the rapid development of Internet technology, Web applications have become an indispensable part of people's lives and work. How to build and deploy web applications more efficiently has also become a hot topic. This article will introduce how to use Golang's web framework Echo framework and Docker to build an efficient web application. 1. About the Echo Framework The Echo Framework is a high-performance web framework written in Golang. It is characterized by being lightweight, simple, easy to use and efficient. ByEch

php提交表單通過(guò)后,彈出的對(duì)話(huà)框怎樣在當(dāng)前頁(yè)彈出php提交表單通過(guò)后,彈出的對(duì)話(huà)框怎樣在當(dāng)前頁(yè)彈出而不是在空白頁(yè)彈出?想實(shí)現(xiàn)這樣的效果:而不是空白頁(yè)彈出:------解決方案--------------------如果你的驗(yàn)證用PHP在后端,那么就用Ajax;僅供參考:HTML code

As a fast and efficient programming language, Go language has always been favored by programmers. In the Go language ecosystem, frameworks play a vital role in helping developers build applications faster. This article will introduce five Go language frameworks to let you understand their characteristics and usage. 1. Gin framework The Gin framework is a lightweight Web framework with fast and high performance characteristics. Use the Gin framework to quickly build RESTful APIs and web applications. Here is a simple example code:
