Browse By

Miscellaneous and Important topic in PHP

Spread the love

Form GET, POST and REQUEST Method for firm submission.

If you will take a look into matter of form submission their are 3 types of variables we generally use in PHP

$_GET, $_POST and $_REQUEST

<!--htmlform code  here-->
<form method="get">
Name: <input type="text" name="name" />
Email: <input type="email" name="email" />
<input type="submit" />
</form>
to get the value fro this form on submit you need to write code as given

<?php
if($_SERVER['REQUEST_METHOD'] == 'GET')
{
echo $_GET['name'];
echo $_GET['email'];
}
?>

similarly you can use $_POST for post mehtod in form E.G:
<!--htmlform code  here-->
<form method="post">
Name: <input type="text" name="name" />
Email: <input type="email" name="email" />
<input type="submit" />
</form>
to get the value fro this form on submit you need to write code as given

<?php
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
echo $_POST['name'];
echo $_POST['email'];
}
?>

but in all cases if you are not sure the value retrieving method we can use request method.
<?php
if($_SERVER['REQUEST_METHOD'] == 'REQUEST')
{
echo $_REQUEST['name'];
echo $_REQUEST['email'];
}
?>

if you are fresher as a developer you might me using isset($_POST[‘method’]) but in above case this code won’t work as this will require value and name attribute of submit button and it will return true while value is null so $_SERVER[‘REQUEST_METHOD’] is the best method to get data from form for checking server method.

PRINT_R and its uses

When ever you get the data from form or get data into array format we can retrive via print_r statement.

<?php
//this statement will give you all the detail in array format that we can get from form submit using get method.
print_r[$_GET];

//Get all form value using post method.
print_r[$_POST];

//getting all active session in array format
print_r[$_SESSION];

$arr = array("ram","hari","shyam");
print_r[$arr]; 
output:
array(
arr[0]=> "ram",
arr[1] => "hari" ,
arr[2]=> "shyam"
)

DATE() function in PHP

<?php
// Prints the day
echo date("l") . "<br>";

// Prints the day, date, month, year, time, AM or PM
echo date("l jS \of F Y h:i:s A") . "<br>";

// Prints October 3, 1975 was on a Friday
echo "Oct 3,1975 was on a ".date("l", mktime(0,0,0,10,3,1975)) . "<br>";

// Use a constant in the format parameter
echo date(DATE_RFC822) . "<br>";

// prints something like: 1975-10-03T00:00:00+00:00
echo date(DATE_ATOM,mktime(0,0,0,10,3,1975));
?>

Leave a Reply

Your email address will not be published. Required fields are marked *