Home Backend Development PHP Tutorial What is a php form? How to create a php form (example)

What is a php form? How to create a php form (example)

Jul 24, 2018 pm 03:04 PM
php form

The php form is an area containing form elements. You can create a form by using the

tag and inserting relevant form elements into it. Below I will give you a detailed analysis of how to create a form.

Form elements are elements that allow users to enter information in a form (such as text fields, drop-down lists, radio buttons, check boxes, etc.).

Forms are defined using the form tag ().

<form action="script.php" method="post"> 
</form>
Copy after login

As far as PHP is concerned, the most important attribute of the form tag is action, which specifies which page the form data will be sent to. If it is empty, it will be submitted to the page containing the form. That is the current page;
The second attribute is method, which specifies how to send data to the processing page. The two options (get and post) indicate the HTTP method to be used.

method method selection:

##The get method is to pass the submitted data through a series of The (name-value) pair appended to the URL is sent to the receiving page. For example:

http://www.example.com/script.php?name=Homer&gender=M&age=35

Unfortunately, the amount of data transferred via get is limited, and it is not very secure ( because the data is visible).

Generally speaking, get is used to request information, such as a specific record in a database or the results of a search (searches almost always use get).

When you need to take an action (such as updating a database record or sending an email), use the post method.

For these reasons, I generally use post, and if exceptions occur, I will point them out separately.


Let’s create a simple form. Since this file does not contain php code, I saved the file with a (.html) suffix. Of course, it can also be used (.php) suffix.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>简单的HTML表单</title>
<style type="text/css">
label {font-weight: bold;color: #300ACC;}
</style>
</head>

<body>
<form action="demo1.php" method="post">

	<fieldset><legend>在下面的表格输入您的信息:</legend>
	
	<p><label>姓名: <input type="text" name="name" size="20" maxlength="40" /></label></p>
	
	<p><label>邮箱地址: <input type="text" name="email" size="40" maxlength="60" /></label></p>
	
	<p><label for="gender">性别: </label><input type="radio" name="gender" value="M" /> 男 <input type="radio" name="gender" value="F" /> 女</p>
	
	<p><label>年龄:
	<select name="age">
		<option value="0-29">30岁以下</option>
		<option value="30-60">30岁 到 60岁 之间</option>
		<option value="60+">60岁以上</option>
	</select></label></p>
	
	<p><label>评论: <textarea name="comments" rows="3" cols="40"></textarea></label></p>
	
	</fieldset>
	
	<p align="center"><input type="submit" name="submit" value="提交信息" /></p>

</form>
</body>
</html>
Copy after login
Such a form is ready. The final rendering is as follows:

Related recommendations:

What is a PHP form and form creation

How to build a PHP form?

The above is the detailed content of What is a php form? How to create a php form (example). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use two-factor authentication in PHP forms to improve security How to use two-factor authentication in PHP forms to improve security Jun 24, 2023 am 09:41 AM

Nowadays, in the era of digitalization and networking, security has become one of the important factors that cannot be ignored in the Internet world. Especially in business scenarios with high data sensitivity, how to improve the security of websites, applications and user data is particularly important. Using two-step authentication in PHP forms to enhance security is a feasible solution. Two-Factor Authentication (2FA), also known as double authentication and multi-factor authentication, refers to the process where the user completes the regular account password.

PHP form protection tips: How to prevent repeated form submissions PHP form protection tips: How to prevent repeated form submissions Jun 24, 2023 am 11:50 AM

When using PHP forms for data submission, the problem of repeated form submission often occurs. This can lead to inaccurate data or, worse, system crashes. Therefore, it is very important to understand how to prevent duplicate submissions. In this article, I will introduce some PHP form protection techniques to help you effectively prevent repeated form submission problems. 1. Add a token to the form Adding a token to the form is a common way to prevent repeated submissions. The principle of this method is to add a hidden field to the form, which contains

How to use PHP forms to prevent CSRF attacks How to use PHP forms to prevent CSRF attacks Jun 24, 2023 am 11:53 AM

With the continuous development of network technology, security issues have increasingly become an issue that cannot be ignored in network application development. Among them, the cross-site request forgery (CSRF) attack is a common attack method. Its main purpose is to use the user to initiate an illegal request to the background by allowing the user to initiate a malicious request in the browser when the user is logged in to the website. This leads to server-side security vulnerabilities. In PHP applications, using form validation is an effective means of preventing CSRF attacks. Add CSRFToken to verify CSRF attacks

How to handle mobile and responsive design in PHP forms How to handle mobile and responsive design in PHP forms Aug 10, 2023 am 11:51 AM

How to deal with mobile and responsive design in PHP forms. With the popularity and frequency of mobile devices increasing, and more and more users using mobile devices to access websites, adapting to mobile has become an important issue. When dealing with PHP forms, we need to consider how to achieve a mobile-friendly interface and responsive design. This article explains how to handle mobile and responsive design in PHP forms and provides code examples. 1. Responsive forms using HTML5 HTML5 provides some new features that can easily implement responsive forms.

How to handle multilingual input in PHP forms How to handle multilingual input in PHP forms Aug 10, 2023 pm 09:00 PM

How to handle multi-language input in PHP forms With the development of globalization, multi-language support for websites has become a necessary feature. In PHP development, how to handle multi-language input so that users can enter data in different languages ​​​​in the form and store and display the data correctly is an important issue that developers need to solve. This article will introduce how to use PHP to handle multi-language input and give corresponding code examples. 1. Set the language setting of the website In the PHP code, we can use setlocale

How to add user operation records to PHP forms to improve security How to add user operation records to PHP forms to improve security Jun 24, 2023 am 09:13 AM

With the rapid development of Internet technology, more and more websites use PHP forms to collect user information. However, the risks that come with it are getting higher and higher, because hackers may use the form to capture users' private information or conduct malicious attacks. In order to prevent these risks, we need to add user operation records to PHP forms to improve security. 1. What is user operation record? User operation record is a record of all operations performed by each user when using the system, including logging in, registering, filling out forms, submitting forms, etc. These records can be used to track

How to prevent SQL injection attacks using PHP forms How to prevent SQL injection attacks using PHP forms Jun 24, 2023 am 08:17 AM

SQL injection attack is a relatively common method of network attack at present. It refers to constructing illegal SQL statements to achieve illegal operations on the database, thereby obtaining sensitive information, destroying data, etc. In PHP applications, forms are used as a means of inputting data on the front end, and the data entered in the form is likely to be used as a component of a SQL query statement. Therefore, preventing SQL injection attacks is crucial to the security of the application. This article will introduce how to use PHP forms to prevent SQL injection attacks. 1. What is SQL injection?

How to add data desensitization to PHP forms to improve security capabilities How to add data desensitization to PHP forms to improve security capabilities Jun 24, 2023 am 10:24 AM

In this digital age, data security has become increasingly important. Whether you are an individual user or a business user, you need to consider how to protect your data from attacks. PHP forms are commonly used tools for collecting data, but many people don’t know how to add data desensitization to forms to improve the security of form data. This article will explain how to add data desensitization to PHP forms to improve the security capabilities of form data. What is data desensitization? Simply put, data desensitization is to encrypt or replace sensitive data so that the original

See all articles