How to retrieve html form data with php

HTML khung data can be retrieved và processed in many different ways, for example

by using a scripting language,a server-side language such as PHP,or any of the many programming languages of choice.

Bạn đang xem: How to retrieve html form data with php

In this tutorial, we"re going to walk you through on how to lớn access or retrieve khung data with PHPhường, và show you the different methods that can be used.

Setting up the HTML form

To mix up a form for VPS processing & data retrieval, two important size attributes that controls how the form data is processed whenever it is submitted must be specified. These two size attributes are:

Method Attributeskích hoạt Attributes

...

Action Attributes: specifies the PHPhường script file location for processing when it is submitted. If no script file location is specified, the browser submits the khung by using the current PHP script tệp tin location ( the self-script in which the size is being called ).

Method Attributes:specifies what type of method the khung will use lớn send the data. We have two methods, the GETPOST.

Note: By default, if no method is specified, the GET method is used.

Setting access keys for the form data by using the element"s name attribute

The element"s name attribute ( name="unique-name-here" ) value is used by PHP.. as key khổng lồ enableaccess lớn the data value of the specified khung field element when you submit the size. Without the name attribute specified for each element contained in the form, PHPhường will be unable lớn create an array automatically with an access key by using the element"s name attribute value. This means you can"t access that element form data value after the khung has been submitted lớn the hệ thống because its key is undefined.

How khung data is sent

When you submit the khung lớn the server, it encodes it by using a scheme called URL encoding which has a built-in pattern that describes how the khung data is parsed & encoded. This scheme parses & encodes the khung data as a name/value pairs, & it uses the equal sign (=) to concatenate the name/value pairs together.

name=value

However, if the khung data to be sent consists of different pairs, the ampersand character (&) is used lớn separate them.

name1=firstValue&name2=secondValue&name3=thirdValue

Also, if the size data khổng lồ be sent contains the space character, the scheme replaces it with the plus character (+), & every other non-word characters present is encoded differently.

How send works

Client browsers can skết thúc information to a web server in two different ways:

GET MethodPOST Method

The GET Method

This method instructs the browser to lớn send the encoded information (the name/value pairs) through the URL parameter by appending it khổng lồ the page request. The browser implement this method by concatenating the question mark character (?) to the over of the page request since it specifies where the query string (name/values pairs) starts from, và all the size data is visible lớn everyone as it is displayed in the browser address bar.

Example: How the size GET method data is submitted

*

URL parameters explained

http://localhost/example.com: Specifies the page request. It is the page the browser is requesting from the hệ thống.?: This character specifies where the query string for the requested page starts from. When it is omitted, the browser won"t understvà how to handle và send the query string (name/values pairs) khổng lồ the VPS.name1, name2: Specifies the size field element"s name attribute value. Each is assigned khổng lồ its corresponding form field data, & it is used as the access key by the hệ thống script (PHP), to retrieve its data value when you fill out the size và submit it.firstValue, secondValue: These are the inputted values you entered before submitting the khung. Each Value is assigned to its corresponding element"s name attribute value.&: This character is used to lớn concatenate the name/value pairs together as one long query string.=: This character is used lớn assign the name of the form field element to lớn its data value as a name/value pair.

How to lớn retrieve sầu form data sent via GET

When you submit a khung through the GET method, PHPhường. provides a superglobal variable, called $_GET. PHPhường uses this $_GET variable khổng lồ create an associative array with keys khổng lồ access all the sent information ( khung data ). The keys is created using the element"s name attribute values.

The $_GET Method Script: get-method.php

// Check if the size is submitted if ( isset( $_GET<"submit"> ) ) // retrieve the khung data by using the element"s name attributes value as key $firstname = $_GET<"firstname">; $lastname = $_GET<"lastname">; // display the results eđến "

Form GET Method

"; emang đến "Your name is " . $lastname . " " . $firstname; exit;

The GET method

Here is an image showing the code output:

*

Now let"s take a look at the code ...

Xem thêm: Chơi Game Anh Hùng Trái Đất 2, Chơi Game Anh Hùng Trái Đất Online

The PHP isset() function is used to determine if a variable is phối và is not null.

Firstly, the isset() function checks if the size has been submitted by using the element"s name attribute value "submit" (name="submit") as key và pass it lớn the $_GET<> superglobal variable. This is because the form data are stored in the $_GET<> superglobal array by PHP. when it is submitted through the GET method.

Then the form field, first name và last name khung data are retrieved by using the same method, passing their respective sầu name attribute values inkhổng lồ the $_GET<"name as key"> array parameter, & each is assigned lớn a variable name that was used khổng lồ display the results.

Using the POST

The size POST method sends information via HTTPhường. header. All name/value pairs sent through this method is invisible lớn anyone else since all the information are embedded within the toàn thân of the HTTP. request.

When you submit a size lớn a server through the POST method, PHP provides a superglobal variable called $_POST. The $_POST variable is used by PHP to lớn create an associative array with an access key ($_POST<"name as key">). The key is created automatically by PHP when the size is submitted. PHPhường uses the form field element name attribute (name="unique-name-here") to lớn create the key.

The $_POST Method Script: post-method.php

// Check if the khung is submitted if ( isset( $_POST<"submit"> ) ) // retrieve sầu the size data by using the element"s name attributes value as key $firstname = $_POST<"firstname">; $lastname = $_POST<"lastname">; // display the resultsemang đến "

Form POST Method

"; eđến "Your name is " . $lastname . " " . $firstname; exit;

The POST method form

*

Code explained

isset( $_POST<"submit"> ) : This line checks if the form is submitted using the isset() function, but works only if the size input đầu vào type submit has a name attribute (name="submit"). $_POST<"firstname">: The khung data is stored in the $_POST<"name as key"> variable array by PHP since it is submitted through the POST method, & the element name attribute value - firstname (name="firstname") is used lớn access its size field data. The same procedure is used for $_POST<"lastname">. The form data is then assigned to a variable that was used to lớn display the results.

The $_REQUEST variable

The $_REQUEST variable is another PHPhường superglobal variable that you can use khổng lồ dynamically retrieve sầu khung data sent from both Form GET and POST methods. The $_REQUEST variable contains the content of both $_GET, $_POST và $_COOKIES.

Note: the $_COOKIES superglobal variable is used for creating COOKIES data. We will discuss this in a different tutorial.

Example: request-script.php

The $_REQUEST variable code

// Cheông xã if the khung is submittedif ( isset( $_POST<"submit"> ) ) // retrieve sầu the khung data by using the element"s name attributes value as keyeđến "

size data retrieved by using the $_REQUEST variable"$firstname = $_REQUEST<"firstname">;$lastname = $_REQUEST<"lastname">;// display the resultsecho "Your name is " . $lastname ." " . $firstname;// check if the post method is used khổng lồ submit the formif ( filter_has_var( INPUT_POST, "submit" ) ) emang đến "size data retrieved by using $_POST variable"$firstname = $_POST<"firstname">;$lastname = $_POST<"lastname">;// display the resultsemang lại "Your name is " . $lastname ." " . $firstname;// kiểm tra if the get method is used khổng lồ submit the formif ( filter_has_var( INPUT_GET, "submit" ) ) emang đến "form data retrieved by using $_GET variable"$firstname = $_GET<"firstname">;$lastname = $_GET<"lastname">;// display the resultsemang đến "Your name is " . $lastname ." " . $firstname;exit;

The khung code

*

Code explained

The $_REQUEST variable script code works exactly the same way as the previous $_GET & $_POST code script above. The only task required is khổng lồ replace the $_GET và $_POST with the $_REQUEST variable.

The filter_has_var() function

It checks if a variable of a specified input đầu vào type exists. It has two parameters, filter_has_var( type, variable_name ), & both parameters are required. The first parameter type specifies the đầu vào type lớn kiểm tra for, which can be any of the following constant values ( INPUT_GET, INPUT_POST, INPUT_COOKIE, INPUT_SERVER, INPUT_ENV). The second parameter input specifies the variable name (the name attribute of the khung input element, name="unique-name-here") lớn check.

Anatomy of the GET method

The GET method produces a long query string that is displayed in the browser"s address bar when the size is submitted.The GET method should not be used to send sensitive content/information like password because all of the content/information is displayed in the browser"s address bar.When sending form data through the GET method, you can only skết thúc a maximum of 2048 characters.The GET method cannot be used to skết thúc binary data like images, mp3 or pdf files khổng lồ the hệ thống.When you submit a form through the GET method, PHP creates a $_GET associative sầu array in this format, $_GET<"name as key"> lớn enable you lớn retrieve sầu the khung data.The GET method is suitable lớn send non-sensitive sầu content/information to lớn the server.

Anatomy of the POST method

The POST method sends information via HTTP header, all the information are embedded within the toàn thân of the HTTP request.The POST method can be used to skết thúc sensitive sầu content/information since all data are sent through the HTTP header.This method has no limit on the amount of information lớn sover to the VPS.The POST method provides support khổng lồ send binary data like images, mp3 or pdf files, and also provides enhancement for tệp tin uploading to the hệ thống.When you submit a form through the POST method, PHPhường creates a $_POST associative sầu array in this format, $_POST<"name as key"> to enable youlớn retrieve sầu the form data.

Form validation

We are going to filter & sanitize the inputted data by using the PHP preg_replace() function. Although, the PHP. filter extension can be used khổng lồ perform the same task.

//

The Preg_replace() function

This function performs a regular expression tìm kiếm và replace.

preg_replace( $pattern, $replacement, $subject, $limit, $count )

Note: The $limit và $count parameters are optional. Both can be omitted.

Example: The preg_replace() function code

$firstname = preg_replace( "#<^w>#", "", $_POST<"firstname"> );$lastname = preg_replace( "#<^w>#", "", $_POST<"lastname"> );

Code explained

The first parameter: "#<^w>#" represents a Regular Expression pattern, the function uses this pattern and validates it against a Regular Expression word character (w). w is a Regex metacharacter that matches only word characters ( the alphabet A lớn Z uppercase or a lớn z lowercase, và the underscore character _ ). The <^w> matches any non-word characters. The hash character (#) is the regular expression modifier.

The second parameter: "" is what the function will use to replace any non-word characters found in the inputted values. In this case, we"re replacing any nonword characters found with an empty string.

Xem thêm: Free Ftp Web Hosting - Free Unlimited Web Hosting With Php, Mysql, Ftp

The third parameter: $_POST<"firstname"> or $_POST<"lastname"> represents the inputted values you entered into the khung field. The function checks the characters that is contained in this third parameter, & then validates it against the expression (match any non-word character) in the first parameter, if any non-word character is found, the function replaces it with the value in the second parameter (an empty string).


Chuyên mục: Theme wordpress