Hướng dẫn dùng w3schools form trong PHP
The PHP superglobals $_GET and $_POST are used to collect form-data. Show Nội dung chính
PHP – A Simple HTML Form
The example below displays a simple HTML form with two input fields and a submit button: Example
Run Example » When the user fills out the form above and clicks the submit button, the form data is sent for processing to a PHP file named “welcome.php”. The form data is sent with the HTTP POST method. To display the submitted data you could simply echo all the variables. The “welcome.php” looks like this: Welcome The output could be something like this: Welcome John The same result could also be achieved using the HTTP GET method: Example
Run Example » and “welcome_get.php” looks like this: Welcome The code above is quite simple. However, the most important thing is missing. You need to validate form data to protect your script from malicious code. Think SECURITY when processing PHP forms! This page does not contain any form validation, it just shows how you can send and retrieve form data. However, the next pages will show how to process PHP forms with GET vs. POSTBoth GET and POST create an array (e.g. array( key1 => value1, key2 => value2, key3 => value3, …)). This array holds key/value pairs, where keys are the names of the form controls and values are the input data from the user. Both GET and POST are treated as $_GET and $_POST. These are superglobals, which means that they are always $_GET is an array of variables passed to the current script via the URL parameters. $_POST is an array of variables passed to the current script via the HTTP POST method. When to use GET?Information sent from a form with the GET method is visible to everyone (all variable names and values are displayed in the
URL). GET GET may be used for sending non-sensitive data. Note: GET should NEVER be used for sending passwords or other sensitive information! When to use POST?Information sent from a form with the POST method is
invisible to others Moreover POST supports advanced functionality such as support for multi-part binary input while uploading files to server. However, because the variables are not displayed in the URL, it is not possible to bookmark the page. Developers prefer POST for sending form data. Next,
lets see how we can process PHP forms the PHP ExercisesTest Yourself With ExercisesExercise:If the form in the white section below gets submitted, how can you, in welcome.php, output the value from the “first name” field? Welcome This chapter shows how to keep the values in the input fields when the user hits the submit button. PHP – Keep The Values in The FormTo show the values in the input fields after the user hits the submit button, we add a little PHP script inside the value attribute of the following input fields: name, email, and website. In the comment textarea field, we put the script between the tags. The little script outputs the Then, we also need to show which radio button that was checked. For this, we must manipulate the checked attribute (not the value attribute for radio buttons): Name: ”> E-mail: ”> Website: ”> Comment: Gender: PHP – Complete Form ExampleHere is the complete code for the PHP Form Validation Example: |