Using PHP GET and POST simultaneously via single HTML form

As we know, HTTP POST and HTTP GET are two great means of passing user data to servers. Previously, we have discussed about how to create different pages via a single script using URL variables, ie using GET method. Now, there can be occasions where, you need to have HTTP POST forms to get data from users. The question is, can you use GET and POST method together to create different dynamic pages from a single script? The answer is yes! Just check in the online demo, to see what we are talking about, and advanced users can just download the script to learn by themselves. And hey, if you are new to this, just continuing reading.

Demo[download id=”8″ format=”1″]

Basically, there are many occasions while we face the necessity to use only a single script to provide different pages, and also need to include forms inside those pages to interact with the user via HTTP POST method. The obvious question, arises in such situation is, how we can pass both GET data and POST data from a single form. The GET data is necessary to build the particular page, while the POST data is necessary to make the interaction, and moreover they have to be done simultaneously. So, how to use it then?

Well, practically, the solution is pretty easy. But, before we jump into the solution, make sure you have read our previous article on Understanding the basics of HTML forms and PHP GET and POST method.

#0: The strategy:

  • We will use the action attribute of the form to pass in the GET variables (or URL parameters).
  • We will set the method of the form to post. So all the form field data will be posted to the server.

That is all actually. Now, let us see the actual code.

#1: The HTML form & PHP Code:

We have two files, one simple html file, which will just show in the form and pass the GET and POST data to the PHP file. The other one is the actions.php file, which will utilize the GET and POST data to interact with the user. Let’s see how things fit together.

The contact.html file:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title><!-- Insert your title here --></title>
</head>
<body>
    <h2>Drop In</h2>
    <fieldset>
        <legend>Contact Us</legend>
        <form action="actions.php?pmode=contact" method="post">
            <p>
                <label for="user_name_post">Your Name: &raquo;</label>
                <input type="text" name="user_name_post" id="user_name_post" value="Enter name" />
            </p>
            <p>
                <label for="user_email_post">Your Email: &raquo;</label>
                <input type="text" name="user_email_post" id="user_email_post" value="yourname@company.com" />
            </p>
            <p>
                <label for="user_subj_post">Subject: &raquo;</label>
                <input type="text" name="user_subj_post" id="user_subj_post" value="Enter Subject" />
            </p>
            <p>
                <label for="user_text_post">Message? &raquo;</label>
                <textarea name="user_text_post" id="user_text_post"></textarea>
            </p>
            <p>
                <input type="submit" value="Send" />
            </p>
        </form>
    </fieldset>
</body>
</html>

The register.html file:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title><!-- Insert your title here --></title>
</head>
<body>
    <h2>Register Below</h2>
    <fieldset>
        <legend>Register Now</legend>
        <form action="actions.php?pmode=register" method="post">
            <p>
                <label for="user_name_post">Your Name: &raquo;</label>
                <input type="text" name="user_name_post" id="user_name_post" value="Enter name" />
            </p>
            <p>
                <label for="user_age_post">Your Age: &raquo;</label>
                <input type="text" name="user_age_post" id="user_age_post" value="Age in Years" />
            </p>
            <p>
                <label for="user_sex_post">Your Sex: &raquo;</label>
                <select name="user_sex_post" id="user_sex_post">
                    <option value="Male">Male</option>
                    <option value="Female">Female</option>
                </select>
            </p>
            <p>
                <label for="user_email_post">Your Email: &raquo;</label>
                <input type="text" name="user_email_post" id="user_email_post" value="yourname@company.com" />
            </p>
            <p>
                <label for="user_company_post">Company: &raquo;</label>
                <input type="text" name="user_company_post" id="user_company_post" value="yourname@company.com" />
            </p>
            <p>
                <label for="user_text_post">Any Message? &raquo;</label>
                <textarea name="user_text_post" id="user_text_post"></textarea>
            </p>

            <p>
                <input type="submit" value="Register" />
            </p>
        </form>
    </fieldset>
</body>
</html>

The actions.php file:

<?php
/**
 * Prepare the POST data to be used directly
 * This is just to remove all the slashes added by PHP magic code gpc.
 * We will remove them using stripslashes
 */
function stripslash_gpc( &$value ) {
    $value = stripslashes( $value );
}
/**
 * Check to see if magicgpc is on or not! If so then stripslase all the POST variable
 * First check if server request method is post
 * Then check if magic code gpc is turned on or not
 */
if($_SERVER['REQUEST_METHOD'] == 'POST') {
    if(get_magic_quotes_gpc())
        array_walk_recursive($_POST, 'stripslash_gpc');
}

/**
 * Get the pmode GET variable
 */
$pmode = $_GET['pmode'];

/**
 * switch the $pmode to make different cases
 */
switch($pmode) {
    /**
     * The contact form case
     */
    case 'contact' :
        /**
         * Check to see if the Request method is POST
         */
        if($_SERVER['REQUEST_METHOD'] == 'POST') {

            /**
             * Do something with the data
             */
            ?>
<div class="response">
    <p>Hi <a href="mailto:<?php echo $_POST['user_email_post']; ?>"><?php echo $_POST['user_name_post']; ?></a>. We have received your email.</p>
    <p>Your Email message was:</p>
    <blockquote>
        <p><strong><?php echo $_POST['user_subj_post']; ?></strong></p>
        <p><?php echo $_POST['user_text_post']; ?></p>
    </blockquote>
</div>
            <?php
        }
        break;
    /**
     * The registor case
     */
    case 'register' :
        /**
         * Check to see if a POST request has been made
         */
        if($_SERVER['REQUEST_METHOD'] == 'POST') {
            /**
             * Do something with the data
             */
            ?>
            <div class="response">
                <p>Thanks for registration. Following credentials have been stored in our database.</p>
                <p>Name: <?php echo $_POST['user_name_post']; ?></p>
                <p>Age: <?php echo $_POST['user_age_post']; ?> years</p>
                <p>Sex: <?php echo $_POST['user_sex_post']; ?></p>
                <p>Email: <?php echo $_POST['user_email_post']; ?></p>
                <p>Company: <?php echo $_POST['user_company_post']; ?></p>
                <p>Message:</p>
                <blockquote>
                    <p><?php echo $_POST['user_text_post']; ?></p>
                </blockquote>
            </div>
            <?php
        }
}
?>

#2: Understanding the main idea:

  • We have used the GET data to make different modes of the actions.php file. In this demo, we have used a switch case statement to get the $_GET[‘pmode’].
  • Now, inside every mode, we have utilized the POST data using $_POST variable.
  • In the code above, we have three files, out of which two are general html file [contact.html and register.html] which do nothing but sending the data to the actions.php file.
  • The actions.php thereafter takes the process and do the necessary thing.

To check, how this thing works, download the package and run them using XAMPP or WAMP. Also, you can check our online demo, to get the output of the advanced code which we are going to share.

#3: An Advanced example:

If the above example was too easy for you, then consider understanding the following code. It should take your attention.

<?php
/**************************************************************
 * PHP Advance Technique to use POST and GET simultaneously
 * @author Swashata <swashata4u@gmail.com>
 * @license GPL2 You are free to do whatever you want
 */
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Demonstation: Using PHP GET and POST together to make Interactive Page</title>
</head>

<body>
    <p style="text-align: center;"><a href="?pmode=main">The Main Page</a> | <a href="?pmode=contact">Contact Page</a> | <a href="?pmode=register">Register Page</a></p>
<?php
/**
 * A function to stripslashes from string
 * To be used if magiccodegpc is turned on in the server
 */
function stripslash_gpc( &$value ) {
    $value = stripslashes( $value );
}
/**
 * Check to see if magicgpc is on or not! If so then stripslase all the POST variable
 * First check if server request method is post
 * Then check if magic code gpc is turned on or not
 */
if($_SERVER['REQUEST_METHOD'] == 'POST') {
    if(get_magic_quotes_gpc())
        array_walk_recursive($_POST, 'stripslash_gpc');
}
/** Define and check the pmode from the GET URL variable */
$pmode = $_GET['pmode'];

if( !$pmode )
    $pmode = 'main';

/** Now actually make the page using the pmode */
switch( $pmode ) {
    default :
    case 'main' :
        /**
         * Check to see if any Server POST request has been made
         */
        if($_SERVER['REQUEST_METHOD'] == 'POST') {
            echo '<div class="response"><p>You have entered your name <strong>' . $_POST['user_name'] . '</strong> via HTTP POST method</p></div>';
        }
        else {
            echo '<p>No POST response yet</p>';
        }
        ?>
        <p>Hi there, Welcome to our site.</p>
        <fieldset>
            <legend>Enter your Name</legend>
            <form action="?pmode=main" method="post">
                <p>
                    <label for="user_name">What is your name? &raquo; </label>
                    <input type="text" name="user_name" id="user_name" value="Name please" />
                </p>
                <p>
                    <input type="submit" value="Submit" />
                </p>
            </form>
        </fieldset>
        <?php
        break;
    case 'contact' :
        if($_SERVER['REQUEST_METHOD'] == 'POST') {
            ?>
        <div class="response">
            <p>Hi <a href="mailto:<?php echo $_POST['user_email_post']; ?>"><?php echo $_POST['user_name_post']; ?></a>. We have received your email.</p>
            <p>Your Email message was:</p>
            <blockquote>
                <p><strong><?php echo $_POST['user_subj_post']; ?></strong></p>
                <p><?php echo $_POST['user_text_post']; ?></p>
            </blockquote>
        </div>
            <?php
        }
        ?>
        <p>Feel Free to drop in</p>
        <fieldset>
            <legend>Contact Us</legend>
            <form action="?pmode=contact" method="post">
                <p>
                    <label for="user_name_post">Your Name: &raquo;</label>
                    <input type="text" name="user_name_post" id="user_name_post" value="Enter name" />
                </p>
                <p>
                    <label for="user_email_post">Your Email: &raquo;</label>
                    <input type="text" name="user_email_post" id="user_email_post" value="yourname@company.com" />
                </p>
                <p>
                    <label for="user_subj_post">Subject: &raquo;</label>
                    <input type="text" name="user_subj_post" id="user_subj_post" value="Enter Subject" />
                </p>
                <p>
                    <label for="user_text_post">Message? &raquo;</label>
                    <textarea name="user_text_post" id="user_text_post"></textarea>
                </p>
                <p>
                    <input type="submit" value="Send" />
                </p>
            </form>
        </fieldset>
        <?php
        break;
    case 'register' :
        if($_SERVER['REQUEST_METHOD'] == 'POST') {
            ?>
        <div class="response">
            <p>Thanks for registration. Following credentials have been stored in our database.</p>
            <p>Name: <?php echo $_POST['user_name_post']; ?></p>
            <p>Age: <?php echo $_POST['user_age_post']; ?> years</p>
            <p>Sex: <?php echo $_POST['user_sex_post']; ?></p>
            <p>Email: <?php echo $_POST['user_email_post']; ?></p>
            <p>Company: <?php echo $_POST['user_company_post']; ?></p>
            <p>Message:</p>
            <blockquote>
                <p><?php echo $_POST['user_text_post']; ?></p>
            </blockquote>
        </div>
            <?php
        }
        ?>
        <p>Register to take full advantage</p>
        <fieldset>
            <legend>Register Now</legend>
            <form action="?pmode=register" method="post">
                <p>
                    <label for="user_name_post">Your Name: &raquo;</label>
                    <input type="text" name="user_name_post" id="user_name_post" value="Enter name" />
                </p>
                <p>
                    <label for="user_age_post">Your Age: &raquo;</label>
                    <input type="text" name="user_age_post" id="user_age_post" value="Age in Years" />
                </p>
                <p>
                    <label for="user_sex_post">Your Sex: &raquo;</label>
                    <select name="user_sex_post" id="user_sex_post">
                        <option value="Male">Male</option>
                        <option value="Female">Female</option>
                    </select>
                </p>
                <p>
                    <label for="user_email_post">Your Email: &raquo;</label>
                    <input type="text" name="user_email_post" id="user_email_post" value="yourname@company.com" />
                </p>
                <p>
                    <label for="user_company_post">Company: &raquo;</label>
                    <input type="text" name="user_company_post" id="user_company_post" value="yourname@company.com" />
                </p>
                <p>
                    <label for="user_text_post">Any Message? &raquo;</label>
                    <textarea name="user_text_post" id="user_text_post"></textarea>
                </p>

                <p>
                    <input type="submit" value="Register" />
                </p>
            </form>
        </fieldset>
        <?php
}
?>
</body>
</html>

A working example is the Online demo of this tutorial and as always you can download the source codes of all 4 files from below.

Demo[download id=”8″ format=”1″]

And that was all. Hope it was useful for you! On our next post, we will discuss, how to use the POST and GET datas properly and what are the precautions to take while dealing with user input data. If you have liked this, then do share, twit, digg, stumble this article and don’t forget to give your comment.

9 comments

  1. Free Logo Design

    I have a simple news script along with a simple members script that will allow members to save news articles (by adding the article IDs to a field under their username). My problem is, I’m not sure how to display the results of their saved articles. I’m able to display all of the articles, even broken up into pages But I can’t for the life of me echo the headlines/links of the articles the user has saved.

    • Swashata Post author

      Well, I cant just tell you without viewing the actual script! The concept is that, you need to query the headline from the db! As i dont know ur db structure, so i cant tell u ๐Ÿ™‚ . If u need my help then u can contact me from the contact link of the nav menu!

  2. Vishnu R G

    not so great post …its a little bit confusing actually ….

    what u did here is made an action page for two form(html) pages …and the action depends upon the variable $_GET[‘pmode’] ?? this is what it is …

    why needed this much confusing ..well i though ur gonna do something like making a page submit data using both GET AND POST .. and here the $_GET[‘pmode’]. better if u tried some dynamic variable’s there ,,

  3. Vishnu R G

    and sorry one importants thing .. The title “Using PHP GET and POST simultaneously via single HTML form” is not related to what u have done .. if u meant the title in ur way then u should have only one html page …

    the title doesnt justify the content …im nt a geek that why i tell this … its really confusing for a beginner

    • Swashata Post author

      Sorry to disappoint Vishnu, but did you actually looked into the advanced example, or the online demo?? What may seem easy for you, ins’t necessary easy for all! ๐Ÿ™‚ So, what I tried was, giving some simple examples to use a single php script for two different form, then use both GET and POST variable within a single script, handling all the GET and POST requests and generating a completely dynamic page based on the requests.

      But, it is my habit to provide an easy as well as an advance example, so beginners having genuine interest can cope up from the very basics! If you are not a beginner user, then simply ignore the basic fundas and read from the advance example ๐Ÿ˜‰ As easy as that!

  4. Jayachandra.C

    I was searching about this process for a long time. Finally I got it. Thank you very much Mr. Swashata.

  5. Pingback: Copy Remote png, jpeg, gif images to your local server using PHP & GD

  6. madhura

    your site is very importance because i am selfstuding. iam ousl.This is wonderful site

Comments are closed.