Different Pages using PHP $_GET variable

The Main Page | Contact Page | Register Page

No POST response yet

  1. <?php
  2. /**
  3.  * Check to see if any Server POST request has been made
  4.  */
  5. if($_SERVER['REQUEST_METHOD'] == 'POST') {
  6.     echo '<div class="response"><p>You have entered your name <strong>' . $_POST['user_name'] . '</strong> via HTTP POST method</p></div>';
  7. }
  8. else {
  9.     echo '<p>No POST response yet</p>';
  10. }
  11. ?>
  1.     <legend>Enter your Name</legend>
  2.     <form action="?pmode=main" method="post">
  3.         <p>
  4.             <label for="user_name">What is your name? &raquo; </label>
  5.             <input type="text" name="user_name" id="user_name" value="Name please" />  
  6.         </p>
  7.         <p>
  8.             <input type="submit" value="Submit" />
  9.         </p>
  10.     </form>

Hi there, Welcome to our site.

Enter your Name

Basic Source Codes

  1. /**
  2.  * A function to stripslashes from string
  3.  * To be used if magiccodegpc is turned on in the server
  4.  */
  5. function stripslash_gpc( &$value ) {
  6.     $value = stripslashes( $value );
  7. }
  8. /**
  9.  * Check to see if magicgpc is on or not! If so then stripslase all the POST variable
  10.  * First check if server request method is post
  11.  * Then check if magic code gpc is turned on or not
  12.  */
  13. if($_SERVER['REQUEST_METHOD'] == 'POST') {
  14.         array_walk_recursive($_POST, 'stripslash_gpc');
  15. }
  1. <?php
  2. /**************************************************************
  3.  * PHP Advance Technique to use POST and GET simultaneously
  4.  * @author Swashata <swashata4u@gmail.com>
  5.  * @license GPL2 You are free to do whatever you want
  6.  */
  7. ?>
  8. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  9. <html xmlns="http://www.w3.org/1999/xhtml">
  10. <head>
  11. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  12. <title>Demonstation: Using PHP GET and POST together to make Interactive Page</title>
  13. </head>
  14.  
  15. <body>
  16.     <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>
  17. <?php
  18. /**
  19.  * A function to stripslashes from string
  20.  * To be used if magiccodegpc is turned on in the server
  21.  */
  22. function stripslash_gpc( &$value ) {
  23.     $value = stripslashes( $value );
  24. }
  25. /**
  26.  * Check to see if magicgpc is on or not! If so then stripslase all the POST variable
  27.  * First check if server request method is post
  28.  * Then check if magic code gpc is turned on or not
  29.  */
  30. if($_SERVER['REQUEST_METHOD'] == 'POST') {
  31.         array_walk_recursive($_POST, 'stripslash_gpc');
  32. }
  33. /** Define and check the pmode from the GET URL variable */
  34. $pmode = $_GET['pmode'];
  35.  
  36. if( !$pmode )
  37.     $pmode = 'main';
  38.  
  39. /** Now actually make the page using the pmode */
  40. switch( $pmode ) {
  41.     default :
  42.     case 'main' :
  43.         /**
  44.          * Check to see if any Server POST request has been made
  45.          */
  46.         if($_SERVER['REQUEST_METHOD'] == 'POST') {
  47.             echo '<div class="response"><p>You have entered your name <strong>' . $_POST['user_name'] . '</strong> via HTTP POST method</p></div>';
  48.         }
  49.         else {
  50.             echo '<p>No POST response yet</p>';
  51.         }
  52.         ?>
  53.         <p>Hi there, Welcome to our site.</p>
  54.         <fieldset>
  55.             <legend>Enter your Name</legend>
  56.             <form action="?pmode=main" method="post">
  57.                 <p>
  58.                     <label for="user_name">What is your name? &raquo; </label>
  59.                     <input type="text" name="user_name" id="user_name" value="Name please" />  
  60.                 </p>
  61.                 <p>
  62.                     <input type="submit" value="Submit" />
  63.                 </p>
  64.             </form>
  65.         </fieldset>
  66.         <?php
  67.         break;
  68.     case 'contact' :
  69.         if($_SERVER['REQUEST_METHOD'] == 'POST') {
  70.             ?>
  71.         <div class="response">
  72.             <p>Hi <a href="mailto:<?php echo $_POST['user_email_post']; ?>"><?php echo $_POST['user_name_post']; ?></a>. We have received your email.</p>
  73.             <p>Your Email message was:</p>
  74.             <blockquote>
  75.                 <p><strong><?php echo $_POST['user_subj_post']; ?></strong></p>
  76.                 <p><?php echo $_POST['user_text_post']; ?></p>
  77.             </blockquote>
  78.         </div>
  79.             <?php
  80.         }
  81.         ?>
  82.         <p>Feel Free to drop in</p>
  83.         <fieldset>
  84.             <legend>Contact Us</legend>
  85.             <form action="?pmode=contact" method="post">
  86.                 <p>
  87.                     <label for="user_name_post">Your Name: &raquo;</label>
  88.                     <input type="text" name="user_name_post" id="user_name_post" value="Enter name" />
  89.                 </p>
  90.                 <p>
  91.                     <label for="user_email_post">Your Email: &raquo;</label>
  92.                     <input type="text" name="user_email_post" id="user_email_post" value="yourname@company.com" />
  93.                 </p>
  94.                 <p>
  95.                     <label for="user_subj_post">Subject: &raquo;</label>
  96.                     <input type="text" name="user_subj_post" id="user_subj_post" value="Enter Subject" />
  97.                 </p>
  98.                 <p>
  99.                     <label for="user_text_post">Message? &raquo;</label>
  100.                     <textarea name="user_text_post" id="user_text_post"></textarea>
  101.                 </p>
  102.                 <p>
  103.                     <input type="submit" value="Send" />
  104.                 </p>  
  105.             </form>
  106.         </fieldset>
  107.         <?php
  108.         break;
  109.     case 'register' :
  110.         if($_SERVER['REQUEST_METHOD'] == 'POST') {
  111.             ?>
  112.         <div class="response">
  113.             <p>Thanks for registration. Following credentials have been stored in our database.</p>
  114.             <p>Name: <?php echo $_POST['user_name_post']; ?></p>
  115.             <p>Age: <?php echo $_POST['user_age_post']; ?> years</p>
  116.             <p>Sex: <?php echo $_POST['user_sex_post']; ?></p>
  117.             <p>Email: <?php echo $_POST['user_email_post']; ?></p>
  118.             <p>Company: <?php echo $_POST['user_company_post']; ?></p>
  119.             <p>Message:</p>
  120.             <blockquote>
  121.                 <p><?php echo $_POST['user_text_post']; ?></p>
  122.             </blockquote>
  123.         </div>
  124.             <?php
  125.         }
  126.         ?>
  127.         <p>Register to take full advantage</p>
  128.         <fieldset>
  129.             <legend>Register Now</legend>
  130.             <form action="?pmode=register" method="post">
  131.                 <p>
  132.                     <label for="user_name_post">Your Name: &raquo;</label>
  133.                     <input type="text" name="user_name_post" id="user_name_post" value="Enter name" />
  134.                 </p>
  135.                 <p>
  136.                     <label for="user_age_post">Your Age: &raquo;</label>
  137.                     <input type="text" name="user_age_post" id="user_age_post" value="Age in Years" />
  138.                 </p>
  139.                 <p>
  140.                     <label for="user_sex_post">Your Sex: &raquo;</label>
  141.                     <select name="user_sex_post" id="user_sex_post">
  142.                         <option value="Male">Male</option>
  143.                         <option value="Female">Female</option>
  144.                     </select>
  145.                 </p>
  146.                 <p>
  147.                     <label for="user_email_post">Your Email: &raquo;</label>
  148.                     <input type="text" name="user_email_post" id="user_email_post" value="yourname@company.com" />
  149.                 </p>
  150.                 <p>
  151.                     <label for="user_company_post">Company: &raquo;</label>
  152.                     <input type="text" name="user_company_post" id="user_company_post" value="Company name" />
  153.                 </p>
  154.                 <p>
  155.                     <label for="user_text_post">Any Message? &raquo;</label>
  156.                     <textarea name="user_text_post" id="user_text_post"></textarea>
  157.                 </p>
  158.                
  159.                 <p>
  160.                     <input type="submit" value="Register" />
  161.                 </p>  
  162.             </form>
  163.         </fieldset>
  164.         <?php
  165. }
  166. ?>
  167. </body>
  168. </html>