Friday 25 November 2011

PHP Form Handling

  • In the php $_GET and $_POST variables are used to retrieve information from forms, like user input.
  • The most important think to notice when dealing with html forms and php is that any form elements in an html page will automatically be available to your php script.

Ø       The $_GET method

  • The $_GET variable is used to collect the values from a form with method GET.
  • The $_GET variable is an array of variable names and values send by the http GET method.
  • Information sent from a form with the get method is visible to every one and it has limits on the amount of information to send.

Example

          <html>
                    <head>
                              <title>Example of GET method</title>
                    </head>
                    <body>
                             <form method="get" action="welcome_get.php">
                                       User Name : <input type="text" name="uname" />
                                       <br/>
                                      <input type=”submit”>
                             </form>
                    </body>
</html>


Welcome.php

<?php
          echo "Welcome : ".$_GET["uname"]."<br/>";
?>

Output

                        Welcome : Hemangi

  • when using the $_GET variable all variable name and values are displayed in the url.
  • So this method should not be used where sending password or other sensitive information however because the variable are displayed in the url, it Is possible to bookmark the page. This can be useful in some cases.
  • Note that the http get method is not suitable on large variable values, the value can not exceed 100 characters.    

Ø       The $_POST method

  • The $_POST variable is used to collect values from a form with method=post.
  • The $_POST variable is an array of variable names and values sent by the http post method.
  • Information sent from a form with the post method is invisible to others and has no limits on the amount of information to send.

Example

          <html>
                    <head>
                              <title>Example of POST method</title>
                    </head>
                    <body>
                             <form method="post" action="welcome.php">
                                       User Name : <input type="text" name="uname" />
                                       <br/>
                                      <input type=”submit”>
                             </form>
                    </body>
</html>

  • When the user clicks the submit button the url will not contain any form of data and will look something like this.
  • http://localhost/form%20method/welcome_post.php
  • The welcome.php file can now use the $_POST variable to catch the form data.
  •  
Welcome.php

<?php
          echo "Welcome : ".$_POST["uname"]."<br/>";
?>

Output

                        Welcome : Hemangi

  • Variable sent with http post are not shown in the url
  • Variable have no length limit. however because the are not displayed in the url, it is not possible to bookmark the page.
Posted By : Hemangi Zala

No comments:

Post a Comment