search
Homephp教程php手册php教程之表单提交实例

php教程之表单提交实例

Jun 13, 2016 am 10:14 AM
phpbeginnerExamplesubmitTutorialhaveuseofSimpleformovertake

一个超简单的初学者用的php教程之表单提交实例有需要的朋友参考一下。下面我们将创建一个复杂的表单,代码如下所示。


 代码如下 复制代码



































姓名:
密码:
年龄:

自我介绍:


体育爱好:

网球

足球

篮球

保龄球
开发语言:

php

java

perl

c++

.NET

delphi


 

该表单包括了常用表单元素:单行文本框、多行文本框、单选项(radio)、多选项(checkbox),以及多选菜单。下面进行详细的说明。
 
maxlength是与密码文本框关联的属性,它限制用户输入密码的最大长度为10个字符。
 
age列表框是列表菜单,它的命名属性下都有自己的值供选择。selected是一个特定的属性选择元素,如果某个option附加有该属性,在显示时就把该项列为第一项显示。
 
intro文本框中的内容,按照rows和cols显示文字、行和列宽。
 
fave_sport是一组单选按钮(radio),我们要按组命名元素名称,比如这一组单选按钮都叫做fave_sport,用户只可选择一个,发送脚本端也只存在一个值。
 
和单选项一样,所有多选项成员也须有同名的属性,而属性名称需要添加括号[],这样就把多选项的值以数组形式发送给PHP,languages就是这种形式。
 
checked标签是指单选项和多选项中的某个值,默认已经被选择。 上面表单的显示画面如图5-3所示。 图5-3 因为上面HTML中的form表单使用的是POST方法传递数据,所以用户提交的数据会保存到$_POST或$_REQUEST的超级全局数组中,我们根据$_POST数组中的值就可以处理提交的数据。 将上面表单中数据提交到someform.php脚本,该脚本的处理逻辑如下:

 代码如下 复制代码
//通过判断按钮的变量名是否在$_POST中定义,如果有表示该表单已提交<br>if(isset($_POST["btn_submit"])){<br>if (empty($_POST['username'])){<br>echo "您没有输入用户名";<br>exit(0);<br>}<br>if (empty($_POST['password'])){<br>echo "您没有输入密码: ";<br>exit(0);<br>}<br>echo "您的用户名: ".$_POST['user_name']."<br>";<br>echo "您的密码(明文): ".$_POST['password']."<br>";<br>echo "您的年龄: ".$_POST['age']."<br>";<br>if (!empty($_POST['languages'])){<br>echo "您选择的语言为:";<br>//处理用户选择兴趣的checkbox按钮产生的数组<br>foreach ($_POST['languages'] as $lang){<br>echo $lang. "  ";<br>}<br>} else {<br>echo "您没有输入任何兴趣爱好";<br>}<br>if (!empty($_POST['develop_ide'])){<br>echo "您使用的开发工具为:";<br>//处理用户多选开发工具菜单产生的数组<br>foreach ($_POST['develop_ide'] as $ide){<br>echo $ide. "  ";<br>}<br>} else {<br>echo "您没有选择开发工具";<br>}<br>echo "您的自我介绍: ".nl2br($_POST['intro'])."<br>";//nl2br(),在字符串中的每个新行 (n) 之前插入 HTML 换行符 (<br>)<br>";<br>echo "网页隐藏值(通过hidden标签值传递): ".$_POST['from']."<br>";<br>}<br>?>
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function