You are advised to check corresponding YouTube video at the end of this article.
Form processing involves capturing user input from a form and taking some action based on that input. The most common use of form processing in PHP is to collect data submitted by a user and then store it in a database or send it via email.
Here's a general overview of how form processing works in PHP:
- Create a HTML form with input fields for user data.
- Specify the action attribute of the form tag as the URL where the form data will be submitted.
- On the PHP file that the form is submitted to, use the $_POST global variable to retrieve the values of the form fields.
- Sanitize and validate the input data to ensure that it meets certain criteria (e.g. required fields are filled out, email address is valid, etc.).
- If the input data passes validation, take some action with it, such as storing it in a database, sending it via email, or displaying it on a webpage.
- If the input data fails validation, display an error message to the user and allow them to correct their input.
It's important to note that form processing in PHP involves taking security precautions, such as sanitizing and validating user input to prevent SQL injection, cross-site scripting (XSS) attacks, and other security vulnerabilities.
form.html
<form action="grabthings.php" method="post">
<label>Name:</label>
<input type="text" name=name>
<input type="submit" value="Submit">
</form>
This is an HTML form code that sends data to a PHP script called "grabthings.php" using the HTTP POST method.
The form contains one input field, which is a text box where the user can enter their name. The name of this input field is "name" and it will be used as the key for the value entered by the user.
When the user clicks the submit button, the form data is sent to the "grabthings.php" file for processing. In this case, the PHP script will receive the value entered in the text box and can use it for further processing.
grabthings.php
<?php
$get_name = $_POST["name"];
echo "<h2>Data from html form</h2>";
echo "<hr>";
echo "Name: $get_name";
?>
This code processes data submitted from an HTML form using the POST method. The HTML form includes an input field for the user to enter their name and a submit button.
When the user clicks the submit button, the form data is sent to the grabthings.php
file, which is specified in the action
attribute of the form tag.
In the grabthings.php
file, the $_POST
superglobal array is used to retrieve the value entered by the user in the input field. The value is stored in the $get_name
variable.
Finally, the value entered by the user is displayed using the echo
statement. The displayed text includes a heading "Data from html form" and a horizontal line to separate it from the other content on the page. The user's name is displayed below the heading.
<form action="grabthings.php" method="post">
<label>Name:</label>
<input type="text" name=name>
<br><br>
<label>Last Name:</label>
<input type="text" name=lastname>
<br><br>
<label>Years:</label>
<input type="text" name=years>
<br><br>
<input type="submit" value="Submit">
</form>
This HTML form includes three input fields for the user to enter their name, last name, and years. It is set up to use the "POST" method to submit the form data to the "grabthings.php" file on the server when the "Submit" button is clicked. Once the form data is submitted, the PHP script in "grabthings.php" can access the form data through the $_POST superglobal array and process it as needed.
Superglobal arrays are predefined arrays in PHP that are always accessible from any part of the PHP script, including functions and methods. These arrays are prefixed with the dollar sign and an underscore, and they are accessible globally without any special declaration.
The superglobal arrays in PHP include:
- $_SERVER - contains information about the server and execution environment
- $_GET - contains information passed to the script through the URL parameters
- $_POST - contains information submitted through an HTML form using the POST method
- $_FILES - contains information about files uploaded to the script through an HTML form
- $_COOKIE - contains information about cookies set on the client side
- $_SESSION - contains information about the current session
- $_REQUEST - contains information submitted through the GET, POST, and COOKIE methods.
Superglobal arrays are useful for handling user input, processing forms, managing sessions, and handling errors.
<?php
$get_name = $_POST["name"];
$get_last_name = $_POST["lastname"];
$get_years = $_POST["years"];
echo "<h2>Data from html form</h2>";
echo "<hr>";
echo "Name: $get_name <br>";
echo "Last Name: $get_last_name <br>";
echo "Years: $get_years <br>";
?>
The data is stored in variables using the $_POST superglobal array. The variables are then printed out using the echo statement.
The HTML form has three input fields for name, last name, and years. When the form is submitted, the data is sent to the PHP script specified in the "action" attribute of the "form" element, in this case, "grabthings.php". The PHP script uses the $_POST superglobal array to retrieve the values of the input fields, which are stored in the variables $get_name, $get_last_name, and $get_years.
The values of these variables are then printed out using the echo statement, along with some HTML formatting tags to make the output more readable
No comments:
Post a Comment