Home >Backend Development >PHP Tutorial >How to use forms in php?
With the development of Internet technology, forms have become an indispensable part of website development. In PHP programming, forms are also commonly used tools. Forms can collect and submit user-entered data, allowing interaction between the website and the user. In this article, we will discuss about PHP forms.
1. The basic concept of a form
A form is composed of HTML code, which can be used to submit data entered by the user. It contains a variety of different HTML tags, such as forms and input boxes. , drop-down menu, etc.
A basic HTML form looks like this:
<form action="form_processing.php" method="post"> <label for="name">Name:</label> <input type="text" name="name" id="name"> <input type="submit" value="Submit"> </form>
Among them, ff9c23ada1bcecdd1a0fb5d5a0f18437
is the start tag of the form, and its action
attribute specifies A script for back-end processing of form data. The method
attribute specifies the method used to submit the form, which can be POST or GET.
Each input element in the form needs to specify the name
attribute so that the back-end program can obtain the data entered by the user. In the above example, d5fd7aea971a85678ba271703566ebfd
is an input element, the type is text
, name
is name
, it will receive Entered username. 2e1cf0710519d5598b1f0f14c36ba674
The label is used to specify the label of the input box.
2.PHP form processing
Through HTML forms, user input data can already be collected, but these data are not actually processed. In PHP, a backend program is required to process the form and store the data in the database.
First of all, to establish a connection between the front end and the back end, the processing logic for form submission needs to be defined in a PHP file. This script file can be specified in the action
attribute of the form, for example:
<form action="form_processing.php" method="post"> <!-- 表单元素 --> </form>
In this PHP file, you can use the $_POST
super global array to get the submitted Form data. For example, in the above example, we can use the following PHP code to get the name entered by the user:
$name = $_POST['name'];
As you can see, the 'name'## of the
$_POST array The # key corresponds to the
name attribute previously set in HTML. If you need to get multiple form elements, you can use a similar method to get the values of other elements. Note that the
$_POST array can only be used when the submission method is POST.
filter_var() function in PHP:
$email = $_POST['email']; if (filter_var($email, FILTER_VALIDATE_EMAIL)) { // 邮件地址格式正确 } else { // 邮件地址格式不正确 }You can also use
preg_match( ) function to use regular expressions to validate form data:
if (preg_match('/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,}$/', $_POST['email'])) { // 邮件地址格式正确 } else { // 邮件地址格式不正确 }4. Form security Security issues need to be considered when using forms to prevent users from entering arbitrary data and cross-site requests Forgery (CSRF) attacks and other issues. Here are a few suggestions:
The above is the detailed content of How to use forms in php?. For more information, please follow other related articles on the PHP Chinese website!