Php-Programming-Sample-Code-1
Php-Programming-Sample-Code-1
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") {
// 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.
<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>
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.
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.
When the user fills out the form and clicks "Submit", the data is sent to the
server URL specified in the action attribute.