JavaScript Error

You currently have JavaScript disabled on your web browser.

This website uses JavaScript, and This web page needs JavaScript activated to work correctly.

Please active JavaScript on your web browser and then refresh this web page.



PHP Form Handling GET Method



Two common ways for collecting submitted data from a client is by using the GET and/or POST methods. A PHP web page can use one or the other, or both methods at the same time. This document discusses how to use the GET method.

With the GET method, the clients form data is passed from the client to the server as visible string value arguments which it encodes onto a URL. The receiving server script will then need to process that encoded URL string to retrieve the clients form data values.

PHP uses the $_GET superglobal variable to collect the clients form data from an encoded URL. The $_GET superglobal variable is an array of the key/value pairs that are passed to the script via the GET method (example: array(key1=>value1, key2=>value2, key3=>value3, ...)).

The web form which submits and transmits the clients data values can send the encoded URL to itself or to another web page.

Since the URL is encoded with the key/value pair values, it is possible to bookmark the URL, and retain the encoded URL data. This is most useful when submitting client form data via hyperlinks and bookmarks when that client data values are already known (such as from an email or text message) instead of from just web page forms.


When to use GET?

The GET method may be used for sending non-sensitive data from the client to the server. The information sent from a clients form with the GET method is visible to everyone, because all variable names and values (as key/value pairs) are displayed in the encoded URL.

The GET method is limited by the maximum URL string size allowed, since its key/value pairs information are encoded into the URL string. The maximum URL string size allowed is restricted and determined by the client and server, which are typically limited to about 2000 characters total.

Note: The GET method should NEVER be used for sending sensitive information, such as passwords or credit card numbers! You should use the POST method for that.


Example 1 ~ Search Engines:

Most search engine sites use the GET method, which allows someone to bookmark the encoded URL string, as well as to copy and paste the encoded URL string into emails and text messages.

For example:
https://www.bing.com/search?q=current+news

Let's break down the above example encoded URL string:

When the above encoded URL is navigated to, it instructs the Bing search engine to query a search for current news.


Example 2 ~ Vote Form

The following example shows a simple 'User Vote' HTML form which contains three input fields and a submit button:

 
<!DOCTYPE html>
<html>
<body>
    <h1>Submit Your Vote:</h1>
    <p>Question: Do you like this example?</p>	
    <form action="sendVote.php" method="get">
        UserName: <input type="text" name="UserName"> <br>
        Email: <input type="text" name="email"> <br>
        Check For Yes: <input type="checkbox" name="vote"> <br>
        <input type="submit">
    </form>
</body>
</html>


When the user clicks on the Submit button the form calls a different web page and passes to it the forms values using the GET method. The following demonstrates how a PHP web page can acquire and use the values passed to it:

 
<!DOCTYPE html>
<?php

// set default values
$uName = "";
$uEmail = "";
$uVote = "";
$feedbackMessage = "";

// get values
if($_SERVER["REQUEST_METHOD"] == "GET")
{
    $uName = $_GET['UserName'];
    $uEmail = $_GET['email'];
    $uVote = $_GET['vote'];

    // TODO : code function to clean and validate values 

    // TODO : code function to generate and send an email

    // Generate Feedback Message
    $feedbackMessage = "Your vote has now been submitted!";
}

?>
<html>
<body>
    <h1>Thank You <?php echo($uName); ?></h1>
    <p><?php echo($feedbackMessage); ?></p>
</body>
</html>


The above examples are only meant to expose you to the very basics, and demonstrate how to use the GET method for sending client form data from one page to another, and how to use the $_GET superglobal for retrieving and using that data values that were passed.


Combining The Two Examples:

We can combine the above two examples for ease of use. After we have already created the sendVote.php page, we can add pre-configured voter hyperlinks to an email body so that the recipient (i.e. a registered member) can easily vote on something directly from an email simply by clicking on the encoded URL hyperlink for that vote value:

For example, the encoded URL for voting Yes is:

And the encoded URL for voting No is:

Notice how multiple key/value pairs are sent on one encoded URL string. This is accomplished by separating each key/value pair with the ampere sign &

Also notice that the on/off value (in vote=on and vote=off) is the default value for a checkbox input type element whenever the explicit value of that element is not specified. This can easily be changed to Yes/No, or True/False, or 1/0, or any value you desire, just be sure and change your receiving code to anticipate for that variable value type.


Final Thoughts

Thank you for reading, I hope you found this blog post (tutorial) educational and helpful.


(0) pollYesResult
(0) pollNoResult



 
     About   |   Contact Us   |   Privacy   |   Terms & Conditions   |   © 2024 - T&J Divisions, LLC, All Rights Reserved