Data Sending Ways in PHP

Data Sending Ways in PHP

There are some certain ways to send your information or data to the PHP page-

  1. Using Form Methods – Get & Post
  2. Using Cookies & Session
  3. Using Query String / URL Rewriting
  4. Using Hidden form field

Form Methods

The GET Method: The GET method sends information separated by the ? Operator.

Example: http://www.xyz.com/home.html?city=Gurgaon

Please note that the GET method is restricted to send upto 1024 characters (1024 bytes) only. You should not use GET method if you have password or other confidential information to be proceeding over the server.

GET can’t be used to send binary data, like images or word documents, to the server.

The PHP provides $_GET associative array or super global variable to access the data sent using GET method.

Example –GET Method:

Let’s create a HTML form that contain a form element-Text Field

HTML Page: form.html

<form action =”thanks.php” method=”GET”>

Enter Name : <input type=”text” name=”txtName”>

<input type=”submit” value=”Submit”>

</form>

Explanation: Here we have a text field called txtName, basically we wanted to send the value of this field to the PHP page called thanks.php. Make sure your form should contain a button type submit to send information to action page.

Let’s create a page thanks.php in the same directory where we have the above html page

PHP Page: thanks.php

<?php

echo $_GET[‘txtName’];

?>

Meanwhile you can see the same entered value on the browser‘s address bar. Here we have print the same value received through text field named txtName. Make sure the same name to be put inside [ ] of super global variable $_GET as mentioned in HTML Form.

 

The POST Method

The POST method transfers information via HTTP headers.

The POST method does not have any restriction on data size to be sent. So it can be used to send files, images as well as binary data as well.

The PHP provides $_POST super global variable or associative array to access data sent using POST method.

Example –POST Method:

Let’s create a HTML form that contain several form elements

HTML Page: form.html

<form action =”thanks.php” method=”POST”>

Enter Email : <input type=”email” name=”email”>

Enter Password : <input type=”password” name=”pass”>

<input type=”submit” value=”Login”>

</form>

Here we have two fields email field called email, password field called pass, basically we wanted to send these above values to the PHP page called thanks.php

Let’s create a page thanks.php in the same directory where we have the above html page

 

 PHP Page: thanks.php

<?php

echo $_POST[‘email];

echo $_POST[‘pass];

?>

Here we have print the same value received through email and password field respectively.

We can make it little advanced by taking above posted values to the variables. The advantage of using variable will be like we can use some validation and sanitizing data before preceding it to the database.

So the above code can be modified something like this-

<?php

// collect values in the variables separately

$email= $_POST[‘email];

$password= $_POST[‘pass];

// you may use some validations like

if($email==”” || $password==””)

echo “Input required”;

else

echo “Welcome “.$email . “ Your password is “.$password;

?>

The $_REQUEST variable

The PHP $_REQUEST variable contains the contents of both $_GET, $_POST, and $_COOKIE. We will discuss $_COOKIE variable when we will explain about cookies.

The PHP $_REQUEST variable can be used to get the result from form data sent with both the GET and POST methods.

Example

<?php

if( isset($_REQUEST[“name”]) )

{

echo “Welcome “. $_REQUEST[‘name’]. “<br />”;

echo “You are “. $_REQUEST[‘age’]. ” years old.”;

}

?>

 

<html>

<body>

<form action = “ ” method = “POST”>

Name: <input type = “text” name = “name” />

Age: <input type = “text” name = “age” />

<input type = “submit” />

</form>

</body>

</html>

Here we can also use $_PHP_SELF variable in form action that contains the name of self script in which it is being called.

Using Query String

This query string can be passed from one page to another by appending it to the URL. You can pass more than one query string by separating the & sign between the query strings.

A query string can contain two things: the query string ID and its value. If you want to access the query string you can use $_REQUEST variables.

Example: test.html

<html>

<head>

<title>Query String Example</title>

</head>

<body>

<a href=”view.php?city=Gurgaon&state=Haryana”>View Location</a>

</body>

</html>

Let’s create a PHP page view.php which should receive two variables values as follow-

city =Gurgaon, state=Haryana

view.php

<?php

echo “Location is “.$_REQUEST[‘city’] . ” ” .$_REQUEST[‘state’];

?>

 

Enjoy the tutorial? Please spread the word :)

Follow by Email
Facebook
Facebook
Google+2k
Google+
http://hirdeshbhardwaj.com/2017/08/17/data-sending-ways-php-2
YouTube41
YouTube