0% found this document useful (0 votes)
95 views

PHP Unit-5

1) The document discusses HTML forms and handling form data in PHP. It covers creating forms, sending data using GET and POST methods, and receiving data with $_GET, $_POST, and $_REQUEST variables. 2) It also discusses uploading files from forms using the $_FILES variable and validating/moving uploaded files. 3) The last section explains how to send emails from PHP using the mail() function and configuring SMTP settings in php.ini.

Uploaded by

Nidhi Bhati
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
95 views

PHP Unit-5

1) The document discusses HTML forms and handling form data in PHP. It covers creating forms, sending data using GET and POST methods, and receiving data with $_GET, $_POST, and $_REQUEST variables. 2) It also discusses uploading files from forms using the $_FILES variable and validating/moving uploaded files. 3) The last section explains how to send emails from PHP using the mail() function and configuring SMTP settings in php.ini.

Uploaded by

Nidhi Bhati
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

PARUL UNIVERSITY - Faculty of IT & Computer Science

BCA, B.Sc. (IT), IMCA Sem-2


Open Source Technology using PHP (05101204)
Unit-5
Handling Form
[1] HTML Form element & its attributes
HTML form element is used to create a form on a web page where users can enter and submit
data. The basic syntax for creating an HTML form is:
<form action="form-handler.php" method="POST">
<!-- form elements go here -->

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).

Some common attributes of the form element include:


name: specifies the name of the form
enctype: specifies how the form data should be encoded when submitted (e.g.
multipart/form-data for file uploads)
autocomplete: enables or disables autocomplete for form fields
novalidate: disables client-side form validation
target: specifies where to open the form response (e.g. _blank for a new window/tab)

[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".

Faculties: Ravi Nimavat, Sohil Parmar, Hardik Parmar Page 68


PARUL UNIVERSITY - Faculty of IT & Computer Science
BCA, B.Sc. (IT), IMCA Sem-2
Open Source Technology using PHP (05101204)
Example:
<form action="process.php" method="POST">
<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">
</form>

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.

Here are the differences between $_GET, $_POST, and $_REQUEST:

$_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.

$_REQUEST: It is a combination of $_GET, $_POST, and $_COOKIE variables. It can be


used to collect data from a form regardless of the method used to submit the form.
Now, let's look at each of these variables with examples:

(1) Using $_GET variable:


The following HTML form uses the GET method to submit data to a PHP script named
form_handler.php:

<form action="form_handler.php" method="get">


<label for="name">Name:</label>
<input type="text" name="name" id="name">
<input type="submit" value="Submit">
</form>

Faculties: Ravi Nimavat, Sohil Parmar, Hardik Parmar Page 69


PARUL UNIVERSITY - Faculty of IT & Computer Science
BCA, B.Sc. (IT), IMCA Sem-2
Open Source Technology using PHP (05101204)
The PHP script can access the form data using the $_GET variable as follows:

$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

(2) Using $_POST variable:


The following HTML form uses the POST method to submit data to a PHP script named
form_handler.php:

<form action="form_handler.php" method="post">


<label for="email">Email:</label>
<input type="email" name="email" id="email">
<input type="submit" value="Submit">
</form>
$email = $_POST['email'];
echo "Your email is: $email";

(3) Using $_REQUEST variable:


The following HTML form uses the POST method to submit data to a PHP script named
form_handler.php:

<form action="form_handler.php" method="post">


<label for="username">Username:</label>
<input type="text" name="username" id="username">
<input type="submit" value="Submit">
</form>

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.

Faculties: Ravi Nimavat, Sohil Parmar, Hardik Parmar Page 70


PARUL UNIVERSITY - Faculty of IT & Computer Science
BCA, B.Sc. (IT), IMCA Sem-2
Open Source Technology using PHP (05101204)
[4] File uploading
In PHP, you can upload files from a user's computer to a web server using a form with an
input field of type "file". Here is an example form:
<form action="upload.php" method="POST"
enctype="multipart/form-data">
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload File" name="submit">
</form>

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.

Here is an example PHP script that handles file uploads:

<?php
if (isset($_POST["submit"])) {
$targetDir = "uploads/";
$targetFile = $targetDir .
basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType =
strtolower(pathinfo($targetFile,PATHINFO_EXTENSION));

// Check if file already exists


if (file_exists($targetFile)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}

// Check file size


if ($_FILES["fileToUpload"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}

// Allow certain file formats


if($imageFileType != "jpg" && $imageFileType != "png" &&
$imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are
allowed.";
$uploadOk = 0;

Faculties: Ravi Nimavat, Sohil Parmar, Hardik Parmar Page 71


PARUL UNIVERSITY - Faculty of IT & Computer Science
BCA, B.Sc. (IT), IMCA Sem-2
Open Source Technology using PHP (05101204)
}

// Check if $uploadOk is set to 0 by an error


if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if
(move_uploaded_file($_FILES["fileToUpload"]["tmp_name"],
$targetFile)) {
echo "The file ". basename(
$_FILES["fileToUpload"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
}
?>

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.

[5] Mail sending using mail()


To send an email using PHP's mail() function on a XAMPP local server, you need to
configure the SMTP settings in the php.ini file and create a PHP script that uses the mail()
function to send the email.

Here are the steps:

1. Locate the php.ini file:


In XAMPP, the php.ini file is located in the "xampp\php" directory. You can open it using a
text editor.

2. Configure the SMTP settings:


Search for the [mail function] section in the php.ini file and modify the following settings
according to your SMTP server settings:

Faculties: Ravi Nimavat, Sohil Parmar, Hardik Parmar Page 72


PARUL UNIVERSITY - Faculty of IT & Computer Science
BCA, B.Sc. (IT), IMCA Sem-2
Open Source Technology using PHP (05101204)
SMTP = smtp.gmail.com
smtp_port = 587
sendmail_from = [email protected]
sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"
mail.add_x_header = On

Here, we have used the Gmail SMTP server with port 587. Replace "your-email-
[email protected]" with your email address.

3. Configure the sendmail settings:


Search for the sendmail_path setting and set the path to the sendmail.exe file in XAMPP's
sendmail directory:

sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"

Make sure to use double quotes around the path and the -t option.

4. Create a PHP script:


Create a new PHP script in XAMPP's htdocs directory (e.g. htdocs/sendmail.php) and add the
following code:

<?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();

if(mail($to, $subject, $message, $headers)){


echo "Email sent successfully!";
} else{
echo "Email sending failed.";
}
?>

Replace "[email protected]" with the email address of the recipient.


Modify the $subject and $message variables as per your needs. Also, replace "your-email-
[email protected]" with your own email address in the $headers variable.

5. Test the script:


Open a web browser and enter the URL of the PHP script in the address bar (e.g.
http://localhost/sendmail.php). If everything is configured correctly, the script should send an
email to the recipient and display the message "Email sent successfully!".

Faculties: Ravi Nimavat, Sohil Parmar, Hardik Parmar Page 73


PARUL UNIVERSITY - Faculty of IT & Computer Science
BCA, B.Sc. (IT), IMCA Sem-2
Open Source Technology using PHP (05101204)
Note:
Make sure that your SMTP server allows sending email from PHP. Some servers may have
restrictions on outgoing email. Also, make sure to secure your email settings by not including
passwords or other sensitive information in the php.ini file

Faculties: Ravi Nimavat, Sohil Parmar, Hardik Parmar Page 74

You might also like