- Php form submit methods example
- Php form Get method example
- Web method in php
- Learn Latest Tutorials
- Preparation
- Trending Technologies
- B.Tech / MCA
- Javatpoint Services
- Training For College Campus
- GET and POST Method In PHP (GET vs POST)
- What is GET and POST Method In PHP?
- What is Difference Between GET and POST Method In PHP?
- Compare GET vs POST Method in PHP
- Related Articles
- Conclusion
- Inquiries
- Leave a Comment Cancel reply
- PHP GET and POST Methods
- The GET Method
- The POST Method
- PHP — GET & POST Methods
- The GET Method
- The POST Method
- The $_REQUEST variable
Php form submit methods example
We often develop web form to capture user data, form can be submitted using two different methods, post and get.
In this tutorial you will learn how to submit form in php application, and also how to capture data from php form, and then to submit to database.
Let’s understand how to retrieve HTML Form data in PHP web application, we will learn php form submit methods get and post, you will see the difference between get and post method in php form.
Here we design two php form with different method type, will see how to retrieve data when the form is posted or submitted.
Php form Get method example
Following form has two fields and the method is set to GET
Now when the form is submitted, we will be able to capture value in following code.
Note: we have used a isset() function, The isset() function return false if testing variable contains a NULL value.
Notice : when any form is submitted with method=”get” , you will be able to see all submitted values in query string with field name.
Following form has two fields and the method is set to POST
Now we retrieve form data in PHP code when form is submitted with POST method, with post methods form values are posted to server.
Note: you won’t be able to see values in query string like in GET method
So far we have seen how to post form in php application, but above example is not very secure way of submitting form, we must consider how to protect from cross-site request, so people can’t make fake request.
First, we learn how to create anti forgery token in php script.
session_start(); if (empty($_SESSION['requestToken'])) < $_SESSION['requestToken'] = bin2hex(random_bytes(32)); >$receivedToken = $_SESSION['requestToken'];
Now add this token to hidden field of your form.
Now to receive and check if the request is valid or not, use following code.
if (!empty($_POST['requestToken'])) < $receivedToken = $_POST['requestToken']; if (hash_equals($_SESSION['requestToken'], $receivedToken)) < // this is valid request >else < // this is NOT a valid request >>
Here is another good example of how to implement Semi-Automatic CSRF Protection in PHP form post.
Web method in php
Learn Latest Tutorials
Preparation
Trending Technologies
B.Tech / MCA
Javatpoint Services
JavaTpoint offers too many high quality services. Mail us on h[email protected], to get more information about given services.
- Website Designing
- Website Development
- Java Development
- PHP Development
- WordPress
- Graphic Designing
- Logo
- Digital Marketing
- On Page and Off Page SEO
- PPC
- Content Development
- Corporate Training
- Classroom and Online Training
- Data Entry
Training For College Campus
JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected].
Duration: 1 week to 2 week
Like/Subscribe us for latest updates or newsletter
GET and POST Method In PHP (GET vs POST)
Hey there, ITSOURCECODER’S! In this tutorial, we will learn about GET and POST Method In PHP and their Differences, which we can understand with the help of examples.
What is GET and POST Method In PHP?
The GET and POST Method in PHP are the methods through which a client or a user can send information to the server.
GET and POST Methods are the HTTP request methods used inside the tag to send the different values from the form to the server.
HTTP is the protocol that enables the communication between the client and the server, where a browser can be the client, and an application running on a computer system that hosts your website can be the server.
What is Difference Between GET and POST Method In PHP?
The difference Between GET and POST method in PHP is GET method sends the information by appending them to the page request, while POST method sends information via HTTP header.
PHP is a server-side scripting language designed for web development. The GET and POST methods are two ways for a client computer to send information to the web server. These methods help to retrieve information from users by forms.
Compare GET vs POST Method in PHP
When you click the “submit” button, this will be the result:
Related Articles
Conclusion
We have completely discussed what is PHP GET and POST Method and their difference. Also, we provide an example program for the two methods.
I hope this simple tutorial will help you a lot to comply with your requirements in PHP Programming.
Inquiries
By the way, If you have any questions or suggestions about this tutorial, Please feel free to comment below. Thank You!
Leave a Comment Cancel reply
You must be logged in to post a comment.
PHP GET and POST Methods
The html METHOD attribute used to send information to web server in different way. The ways are GET method and POST method.
The GET and POST method both used to send data to web server.
The GET Method
PHP GET method used to send data to web server with site url in address bar. The GET method the information appended to url after ? and send to web server .
The GET method never use when we have sensitive information like password.
The GET method only sent up-to 1024 characters append with url.
The GET method can not used to send image file or binary data to server.
We can use $_GET[] function to retrieve the information from address bar to on web page.
In previous php example, write Method=”GET” in form tags.
In above GET method example image we can see the data in address bar after ?.
Here the http://localhost/firstexample.php is out webpage url and name=”” and password=”” and Submit1=login.
If we use GET method for sent data then the all user can see the data at browser address bar.
Now in above example enter username=”meera” and password=”123″ then submit the button.
Now the address bar look like :
If we want to retrieve name and password values for further use we use $_GET[“element_name”] syntax.
The POST Method
The POST method is used to sent data to web server without appending in url at address bar.
The POST method send information with use of HTTP headers.
The POST has no size limit to sent data to web server.
When we work with sensitive data the POST method is better than GET method.
The POST method send binary data.
We can use $_POST[“element_name”] function to get data from a form.
In above example we use METHOD=”POST” like:
Here, we enter username and password then click submit button. This time we can not seen any information at address bar. The POST method sent information without show in address bar.
When we use sensitive data then the POST method is secure method for send information.
If we want to retrieve name and password values for further use we use $_POST[“element_name”] syntax.
we will learn $_GET[] and $_POST[] functions in next php tutorial.
PHP — GET & POST Methods
There are two ways the browser client can send information to the web server.
Before the browser sends the information, it encodes it using a scheme called URL encoding. In this scheme, name/value pairs are joined with equal signs and different pairs are separated by the ampersand.
name1=value1&name2=value2&name3=value3
Spaces are removed and replaced with the + character and any other nonalphanumeric characters are replaced with a hexadecimal values. After the information is encoded it is sent to the server.
The GET Method
The GET method sends the encoded user information appended to the page request. The page and the encoded information are separated by the ? character.
http://www.test.com/index.htm?name1=value1&name2=value2
- The GET method produces a long string that appears in your server logs, in the browser’s Location: box.
- The GET method is restricted to send upto 1024 characters only.
- Never use GET method if you have password or other sensitive information to be sent to the server.
- GET can’t be used to send binary data, like images or word documents, to the server.
- The data sent by GET method can be accessed using QUERY_STRING environment variable.
- The PHP provides $_GET associative array to access all the sent information using GET method.
Try out following example by putting the source code in test.php script.
"; echo "You are ". $_GET['age']. " years old."; exit(); > ?>
It will produce the following result −
The POST Method
The POST method transfers information via HTTP headers. The information is encoded as described in case of GET method and put into a header called QUERY_STRING.
- The POST method does not have any restriction on data size to be sent.
- The POST method can be used to send ASCII as well as binary data.
- The data sent by POST method goes through HTTP header so security depends on HTTP protocol. By using Secure HTTP you can make sure that your information is secure.
- The PHP provides $_POST associative array to access all the sent information using POST method.
Try out following example by putting the source code in test.php script.
echo "Welcome ". $_POST['name']. "
"; echo "You are ". $_POST['age']. " years old."; exit(); > ?>
It will produce the following result −
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.
Try out following example by putting the source code in test.php script.
"; echo "You are ". $_REQUEST['age']. " years old."; exit(); > ?>
Here $_PHP_SELF variable contains the name of self script in which it is being called.
It will produce the following result −