The $_ POST Function Example
form1.html
<html>
<body>
/* form submitted using ‘post’
method, action specifies
next page which is to be
loaded when button is clicked*/
<form
action="welcome1.php" method="post">
// textbox is
to take user input
Name:
<input type="text" name="fname" />
Age:
<input type="text" name="age" />
// Submit button is
to submit the value to next page
<input
type="submit" />
</form>
</body>
</html>
welcome1.php
<html>
<body>
// $_GET to receive the data sent from form1.html
Welcome
<?php echo $_POST["fname"]; ?>.<br />
You
are <?php echo $_POST["age"]; ?> years old!
</body>
Form.html
<html>
<head>
<title>Process the HTML form
data with the POST
method</title>
</head>
<body>
/* form submitted using ‘post’ method, action specifies next page which
is to be loaded when button is clicked*/
<form name="myform"
action="process.php" method="POST">
//create an hidden textbox
<input type="hidden"
name="check_submit" value="1" />
// textbox is to take user input
Name:
<input type="text" name="Name" /><br />
Password:
<input type="password" name="Password"
maxlength="10"
/><br />
// Use
‘select’ tag to display the various options
Select
something from the list: <selectname="Seasons">
<option
value="Spring"
selected="selected">Spring</option>
<option
value="Summer">Summer</option>
<option
value="Autumn">Autumn</option>
<option
value="Winter">Winter</option>
</select><br
/><br />
Choose
one:
//This will create
radio buttons
<input
type="radio" name="Country" value="USA" /> USA
<input
type="radio" name="Country" value="Canada" />
Canada
<input
type="radio" name="Country" value="Other" />
Other
<br
/>
Choose
the colors:
//This will create
checkbox
<input
type="checkbox" name="Colors[]" value="green"
checked="checked"
/> Green
<input
type="checkbox" name="Colors[]" value="yellow"
/>
Yellow
<input
type="checkbox" name="Colors[]" value="red" />
Red
<input
type="checkbox" name="Colors[]" value="gray"
/>
Gray
<br
/>
// Submit button is to submit
the value to next page
<input
type="submit" />
</form>
</body>
</html>
Process.php
<html>
<body>
<?php
if (array_key_exists('check_submit', $_POST)) {
/*Converts the new line characters (\n) in the text
area into HTML line breaks (the <br /> tag)*/
$_POST['Comments'] = nl2br($_POST['Comments']);
//Check whether a $_GET['Languages'] is set
if ( isset($_POST['Colors']) ) {
$_POST['Colors'] = implode(', ', $_POST['Colors']);
//Converts an array into a single string
}
//Let's now print out the received values in thebrowser
echo "Your name: {$_POST['Name']}<br />";
echo "Your password: {$_POST['Password']}<br />";
echo "Your favourite season: {$_POST['Seasons']}
<br/><br />";
echo "You are from: {$_POST['Country']}<br />";
echo "Colors you chose: {$_POST['Colors']}<br />";
}
else
{
echo "You can't see this page
without submitting the
form.";
}
?>
</body>
</html>
OUTPUT of the
above given Example is as follows:
Date() and time() function in PHP
- The PHP date() function formats a timestamp to a more readable date and time.
- A timestamp is a sequence of characters, denoting the date and/or time at which a certain event occurred.
- Some characters that are commonly used for date and time:
·
m - Represents
a month (01 to 12)
·
Y - Represents
a year (in four digits)
·
l (lowercase
'L') - Represents the day of the week
·
h - 12-hour
format of an hour with leading zeros (01 to 12)
·
i - Minutes
with leading zeros (00 to 59)
·
s - Seconds
with leading zeros (00 to 59)
·
a - Lowercase
Ante meridiem and Post meridiem (am or pm)
Example
<html>
<body>
<?php
// display the date in the format YYYY/MM/DD
echo "Today is " . date("Y/m/d") . "<br>";
// ‘l’ is used to display the day
echo "Today is " . date("l"). "<br>";
// display the time in the format HH:MM:SS
echo "The time is " . date("h:i:sa");
?>
</body>
</html>
OUTPUT of the
above given Example is as follows:
Today
is 2014/08/19
Today is Tuesday
Today is Tuesday
The
time is 12:23:22am