PHP Unit-5
PHP Unit-5
Here, the action attribute specifies the URL of the page that will process the form data and
the method attribute specifies the HTTP method to be used (usually POST or GET).
[2] Send Form data using GET Method & POST Method
When submitting a form in HTML, there are two methods that can be used to send the data to
the server: GET and POST.
The GET method sends the form data as part of the URL in the browser's address bar. This
method is typically used for forms that do not involve changing server-side data or updating
databases. In the HTML form tag, the method attribute is set to "GET".
Example:
<form action="process.php" method="GET">
<label for="name">Name:</label>
<input type="text" id="name" name="name"><br><br>
<label for="email">Email:</label>
<input type="email" id="email" name="email"><br><br>
<input type="submit" value="Submit">
When the user submits this form, the data will be sent to the "process.php" script with the
form data appended to the URL like this:</form>
http://example.com/process.php?name=John+Doe&email=john.doe%40
example.com
The POST method sends the form data as part of the request body, so it is not visible in the
browser's address bar. This method is typically used for forms that involve changing server-
side data or updating databases. In the HTML form tag, the method attribute is set to
"POST".
When the user submits this form, the data will be sent to the "process.php" script as part of
the request body. The data will not be visible in the URL or address bar.
[3] Receive Form data using $_GET, $_POST & $_REQUEST variables
In PHP, there are three superglobal variables that can be used to receive form data, namely
$_GET, $_POST, and $_REQUEST. These variables are used to retrieve user input from
HTML forms, and each has its own characteristics that make them useful in different
situations.
$_GET: It is used to collect data from a form using the GET method. It appends form data to
the URL in the form of query parameters. This makes it easy to share form data and
bookmark pages with the same form data. However, it is not secure as the data is visible in
the URL.
$_POST: It is used to collect data from a form using the POST method. It sends form data as
a separate message from the header, making it more secure than $_GET. However, it cannot
be bookmarked or shared with others.
$name = $_GET['name'];
echo "Hello, $name!";
When the form is submitted, the URL will look something like this:
http://example.com/form_handler.php?name=John
The PHP script can access the form data using the $_REQUEST variable as follows:
$username = $_REQUEST['username'];
echo "Your username is: $username";
Note that the $_REQUEST variable can be used to retrieve data regardless of the form
submission method used. However, it is recommended to use the specific variables ($_GET
or $_POST) depending on the method used to submit the form, for better security and
performance.
The important attributes of the form are the "method" attribute set to "POST", the "enctype"
attribute set to "multipart/form-data", and the "name" attribute of the file input set to
"fileToUpload".
Once the user submits the form, the file can be accessed in PHP using the $_FILES
superglobal variable. The $_FILES variable is an associative array that contains information
about the uploaded file, such as its name, type, size, and temporary location on the server.
<?php
if (isset($_POST["submit"])) {
$targetDir = "uploads/";
$targetFile = $targetDir .
basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType =
strtolower(pathinfo($targetFile,PATHINFO_EXTENSION));
The above script first checks if the form has been submitted using the "submit" button. It then
sets a target directory for the uploaded file, and checks for errors such as file size, file type,
and whether the file already exists. Finally, if there are no errors, it moves the uploaded file
from its temporary location to the target directory using the move_uploaded_file() function.
Note that the script only allows JPG, JPEG, PNG, and GIF file formats, and limits the file
size to 500KB. You can modify these restrictions according to your needs. Also, make sure
that the target directory has appropriate permissions to allow file uploads.
Here, we have used the Gmail SMTP server with port 587. Replace "your-email-
[email protected]" with your email address.
Make sure to use double quotes around the path and the -t option.
<?php
$to = "[email protected]";
$subject = "Test email";
$message = "This is a test email sent from XAMPP local
server.";
$headers = "From: [email protected]" . "\r\n" .
"Reply-To: [email protected]" . "\r\n" .
"X-Mailer: PHP/" . phpversion();