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

Php-Programming-Sample-Code-1

The document provides a basic PHP input form for collecting user details such as first name, middle name, last name, address, and birthdate, which is processed to calculate the user's age. It explains the structure of HTML forms, including key attributes and common elements like input fields, text areas, and selection options. Additionally, it covers form submission methods (GET and POST) and the importance of attributes like name, value, and required for form elements.

Uploaded by

Kj Ortiz
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Php-Programming-Sample-Code-1

The document provides a basic PHP input form for collecting user details such as first name, middle name, last name, address, and birthdate, which is processed to calculate the user's age. It explains the structure of HTML forms, including key attributes and common elements like input fields, text areas, and selection options. Additionally, it covers form submission methods (GET and POST) and the importance of attributes like name, value, and required for form elements.

Uploaded by

Kj Ortiz
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Welcome to

Php
Programming!
<!DOCTYPE html>
<html>
<head>
<title>Basic PHP Input Form</title>
</head>
<body>
<h2>Input Form</h2>
<form method="POST" action="">
First Name: <input type="text" name="first_name" required><br><br>
Middle Name: <input type="text" name="middle_name" required><br><br>
Last Name: <input type="text" name="last_name" required><br><br>
Address: <input type="text" name="address" required><br><br>
Birthdate (YYYY-MM-DD): <input type="date" name="birthdate"
required><br><br>
<input type="submit" value="Submit">
</form>
<!--- example PHP Code --->
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {

// Retrieve form data


$first_name = $_POST['first_name'];
$middle_name = $_POST['middle_name'];
$last_name = $_POST['last_name'];
$address = $_POST['address'];
$birthdate = $_POST['birthdate'];

// Calculate age
$birthdateDateTime = new DateTime($birthdate);
$currentDateTime = new DateTime();
$age = $currentDateTime->diff($birthdateDateTime)->y;
// Output details
echo "<h3>Details Submitted:</h3>";
echo "Full Name: " . $first_name . " " . $middle_name . " " . $last_name . "<br>";
echo "Address: " . $address . "<br>";
echo "Birthdate: " . $birthdate . "<br>";
echo "Age: " . $age . " years old<br>";
}
?>

</body>
</html>
Explanation:

HTML Form: The form captures the user input for first name, middle name,
last name, address, and birthdate. The method used is POST.

PHP Processing: When the form is submitted, the data is processed using
$_POST. The birthdate is passed to a function that calculates the age.

Age Calculation: The calculateAge function uses PHP’s DateTime class to find
the difference in years between the current date and the birthdate.

Output: After submission, it shows the full name, address, birthdate, and
calculated age.
This is a basic template. You can expand it based on your needs (e.g.,
validation, styling, etc.).
HTML Forms

HTML Forms are used to collect user input and send it to a web server for
processing. Forms are an essential part of web applications, where users can
input various types of data, such as text, passwords, emails, dates, and more,
and then submit that data.

Basic Structure of an HTML Form

An HTML form is created using the <form> element. It contains input


elements like text fields, checkboxes, radio buttons, and buttons to submit
the form.
<form action="submit_page.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>


Key Attributes of the <form> Tag:

action: Specifies where the form data should be sent after submission (i.e.,
the URL of the server or script that will handle the data).

Example: action="submit_page.php“

method: Specifies how to send the form data. Common values are:

POST: Sends the form data in the body of the HTTP request. It’s more secure
and is typically used for sensitive data.

GET: Sends the form data appended to the URL. It's less secure but can be
used for non-sensitive data.
Example: method="POST"
Common HTML Form Elements:

<input>: Used to create various types of input fields like text, email,
password, etc.

Text Input: <input type="text" name="username">


Password Input: <input type="password" name="password">
Submit Button: <input type="submit" value="Submit">
<textarea>: For multi-line text input.
<textarea name="message" rows="4" cols="50"></textarea>
<select>: A dropdown menu for selecting one or more options.
<select name="country">
<option value="usa">USA</option>
<option value="canada">Canada</option>
</select>
Radio Buttons and Checkboxes:

Radio Button (allows one selection):


<input type="radio" name="gender" value="male"> Male
<input type="radio" name="gender" value="female"> Female

Checkbox (allows multiple selections):


<input type="checkbox" name="vehicle1" value="Bike"> I have a bike
<input type="checkbox" name="vehicle2" value="Car"> I have a car
Other Attributes of Form Elements:

name: Assigns a name to the form element, which will be used as a key
when the form data is sent to the server.
value: Sets the initial value of form elements like input fields or buttons.
placeholder: Provides a hint or placeholder text inside input fields.
required: Ensures that the user must fill out the field before submitting the
form.

Form Submission Example:

When the user fills out the form and clicks "Submit", the data is sent to the
server URL specified in the action attribute.

You might also like