Friday, 21 March 2014

Working with Form data , $_GET , $_POST Variables Html Forms in Php

Form data variables in php
Working with Form data ,  $_GET , $_POST Variables Html Forms back to basic how we can use it in different elements of in that allow as to process data through php programming language. To sent the data to page In this example i am using same page for posting. You can post same page where you are on currently or at another page. Now let see each elements of html form. Name is something to name the textbox that we can pick up for using and processing user input.



Code for working with Form Data 


<form action = "index.php" method="GET">

Day:<br><input type="text" name="day"><br>

Date:<br><input type="text" name="date"><br>

Year:<br><input type="text" name="year"><br><br>

<input type="submit" value="submit">

</form>

Output

Working with Form data ,  $_GET , $_POST Variables Html Forms in Php
Screenshot1
<from> tag is starting of the form and </from> tag is ending of the form. We also can specify action a method now action is where this form is submit when the submit button is clicked. So, we include submit button in this inside form. Here i want my page to link back to same page to index.php you can also redirect to another page. The method can be GET or POST. Depending upon submitting variables. 

$_GET variable is sending data visible in url sending variable through format of url.

$_POST variable is sending data its send data through http post method.

So, Here i am using GET method then take some input method of text type to display date, time and year. take this value from the user.


Working with $_Get Variables in Php programming language. Html forms we may know html forms are used for getting user input get method is useful for forms. 

Code for Working With $_GET Variables (Screenshot1):-

working with global variables
Screenshot1


<?php



if (isset($_GET['day'])&&isset($_GET['date'])&&isset($_GET['year'])) {

$day = $_GET['day'];

$date = $_GET['date'];

$year = $_GET['year'];

if(!empty($day)&&!empty($date)&&!empty($year)) {

echo 'It is '.$day.' '.$date.''.$year;

}else {

echo 'Fill in all fields.';

}

}

?>



<form action = "index.php" method="GET">



Day:<br><input type="text" name="day"><br>

Date:<br><input type="text" name="date"><br>

Year:<br><input type="text" name="year"><br><br>

<input type="submit" value="submit">

</form>

Output (Screenshot2):-


working with form data, $_GET, $_POST variables
Screenshot2
creating html form explain the action method variable GET & POST. $_GET and $_POST variable of html form we can use it in php programming language. For picking up forms variable to php variable. In this example we are processing three variable in get method date, date and year. From the help of html forms we need value to pick up and processes to php and display to the user in this example.

Now in php what we need to do is firstly check few conditions 

1. Does It exist? or "Has it been submitted?"

example code
if (isset($_GET['day'])&&isset($_GET['date'])&&isset($_GET['year'])) {

It will check whether user had submitted the form that will check submit value of submit button.

2. Is it empty? or "Does value == NULL"

example code
if(!empty($day)&&!empty($date)&&!empty($year))

It will check whether submitted value is empty or null value.

3. Display back to user

example code

echo 'It is '.$day.' '.$date.''.$year;
}else {
echo 'Fill in all fields.';
}
}

If both condition will true it will output the user what the user inputted in the form values. 

Now you will be thinking how does the values of form detected in php variable 

example code
$day = $_GET['day'];
$date = $_GET['date'];
$year = $_GET['year'];

In this way we can save form values into the php variables. $_GET method is useful to grab the values from the form to the php variables the name of the textbox field can be use to grab the values in $_GET method.

Day:<br><input type="text" name="day"> -> $day = $_GET['day'];.

The attribute name of the html form is used into the $_GET method and then it can be declare had php variables.
$_POST Variables in php
Screenshot3


Working with $_POST Variables 


Post data using forms now we already seen get data using forms using post data is because for security. In $_GET variable we were displaying value of from into the url. We were showing value name of the form and its value in our url in $_GET method of the form. You can see the structure of $_GET method variables in Screenshot2 and Screenshot3. But in $_POST variable the url of the page will be the file name not the values or anything will show to the url its the only difference between $_GET and $_POST variable. The $_POST variable is used in long text area like description of anything , in registration form for password because password must not be pass to the url to show user and url had limitation some amount of text we can show in url therefore for the long description we can not use $_GET variable.

Code for With $_POST Variables :-


<?php



$match = 'pass123';



if(isset($_POST['password'])) {

$password = $_POST['password'];

if(!empty($password)) {

if($password==$match){

echo 'That is correct!';

}else{

echo 'That is incorrect!';

}

}else{

echo 'Please enter a password.';

}

}



?>



<form action="index.php" method="POST">

password:<br>

<input type="password" name="password"><br><br>

<input type="submit" value="submit">

</form>

Output (Screenshot4)

variables in php
Screenshot4
variables in html forms
Screenshot5

In this example i am checking whether user password is matched with the static variable $match. You can see how $_POST variable is used same as $_GET Variable just by replacing word GET to POST. All things are the same here but the major difference is of url in $_GET method url can pass form values as well as $_POST variable just include file name thats it.

Now as you compare screenshot5 and Screenshot screenshot2 url you will come to the whats its happening. 



No comments:

Post a Comment

Thanks For Comment Will get you Soon..

About Me

Welcome to Extra Tutorials! My name is Mohammed and I am the 22 year writer, website developer, and photographer behind the blog. Thanks for visiting! Tutorials Jackpot… In addition to Developer, I love to develop websites and I love to write. Starting a php Blog was inevitable for me. What began as a simple way to share all of my Tutorials with friends and family has developed into my Part time job.

Mohammed Padela

WHAT IS PHP PROGRAMMING

WHAT IS PHP PROGRAMMING
WHAT IS PHP PROGRAMMING

Follow Us

Popular Posts

Designed ByBlogger Templates