Monday, 25 June 2012

Submitting Forms Using php



In this tutorial we'll have a look at how to capture form data upon submisssion using php scripts.

We will cover the following:

  •  Form Element and its Attributes
    • name
    • action
    • method
  •  GET Method versus POST method
  •   $_GET ,$_POST and $_REQUEST variables in php


 Form Element and its Attributes




 The form element takes three attributes name,action and method.

  •   name: Specifies the name of the form.
    •    name='myform'


  •   action: Specifies the URL of the page to be loaded upon submission of form.
    • action='URL'

  •   method: Specifies whether form data will sent to server using GET or POST method.
    • method='GET' or method='POST'

Summing up, a form element with all three attributes is defined as such


<form name='NameOfForm' method=' GET ' action='sompage.php'>
// 
<form>

GET Method versus POST method

A form can be submitted using either GET or POST. Before sending form data to server, the data is encoded using these two methods. The difference between the get and post methods is form data encoding.


  • GET method involves long URL(see later) and non-ASCII character repertoires. 
  • GET method is less safe than POST method and should be used only and only if form processing does not have side effects.
  • GET method is ideal when data needs to be bookmarked.
  • GET method  should  not be used if sensitive data like passwords are being sent.
  • POST method produces shorter URL and can handle both ASCII and binary.

  $_GET and $_POST variables in php

  • $_GET and  $ _POST are associative arrays. 
  • $_GET is used to retrieve all data sent using get method .
  • $_POST is used to retrieve all data sent using post method. 
NOTE:   $_REQUEST variable is also used to access data sent using both get and post methods.


Example 1 using GET method:

Consider the form  below:


The user will input his name and comments through this form. On clicking the submit button, a php script will be executed.
The php script captures and displays the  name and  comments entered by the user.


HTML SCRIPT:

  <html>
  <head>
  </head>
  <body>
  <form name='frm' action='myphp.php' method='get' >
    <table style='width:100%;'>
      <tr> <td>Name: <input type='text' name='txt-name' value=''/></td> </tr>    
      <tr>  <td> Comments:   <input type='text' name='txt-cmt' value=''/> </td></tr> 
      <tr>   <td> <input type='submit' name='submit' value='Submit'/>  </td> </tr>
    </table>
  </form>
  </body>
  </html>

HTML Code explained:


The submit button sends the form data to server.
The data is sent to the page URL(myphp.php) specified in the form action,where some operations are performed with the data.(below)


PHP SCRIPT:


<?php
$name=$_GET['txt-name']; //capture name
$comment=$_GET['txt-cmt']; //capture comment
echo "Name: ".$name; //output name
echo "<br/>";
echo  "Comment: ".$comment;  //output comment
?>

PHP Code Explained:

The php script uses $_GET associative array to access data(name and comment) which was sent using GET method , stores them in $name and $comment variables respectively and output them.

Note: Pay attention to the URL when get method is used.
It should be something similar to that displayed below:

http://localhost/myphp.php?txt-name=John&txt-cmt=John%27s+Comment&submit=Submit


Example 2 using POST Method:

This time we will try out the post method.

Steps:


  • Copy and modify the previous html code.
    •  Change method='get' to method='post' and save it.
  • Copy previous PHP script as well. 
    • Change  $name=$_GET['txt-name'] to $name=$_POST['txt-name']and  
    • $comment=$_GET[' txt-cmt ']  $comment=$_POST['txt-cmt'].
  • Save and run your script.
  • Again, pay attention to the URL[http://localhost/myphp.php]. 
    • This time the url is shorter and   form data is not appended to the url, which is why it is a lot more secure than get method.
    • Still not clear why? Consider the following scenario:
      • Suppose you have a login form where users need to type in password and other sensitive infomation; with get method, form data gets cached and someone could easily access them later on. Yet another risk could be that someone might just glance at the user's screen and see the sensitive data appended to the url).
      • Obviously in this case, POST method would be necessary.







Example 3 using $_REQUEST Method:

Steps:

  • Simply modify the php script, instead of $_POST and $_GET from previous examples, use $_REQUEST.(see sample code below)
  • Save and run your script.



<?php
$name=$_REQUEST['txt-name']; //capture name
$comment=$_REQUEST['txt-cmt']; //capture comment
echo "Name: ".$name; //output name
echo "<br/>";
echo  "Comment: ".$comment;  //output comment
?>





Conclusion: $_REQUEST can be used for both get or post method.










Tuesday, 12 June 2012

PHP Basics


 



Are you new to php? Confused where to start?  Well, don't worry you have come to the right place.

 This tutorial will cover the following:

  •  What is php? 
  •  What is required and how to run your php scripts?
  •   Brief overview of webservers Apache and  IIS
  •   Installing Xampp on vista/windows 7. 
  •   A simple example.

What is php? 



  • PHP is short for Hypertext Preprocessor.
  • It is an open source server side scripting language; hence free to download.
  • Its main purpose is  to  make web applications dynamic. 
  • It is normally used with other scripting languages namely Javascript ( a client side language) and HTML.
  • A PHP script is saved with .php or .php3 extension; .php is mostly used.
  • PHP works fine with various database namely 
  • MySQL, Oracle, PostgreSQL etc.




What is required and how to run your php scripts?

Unlike HTML and Javascript, PHP scripts cannot be interpreted solely by the  browsers. For executing PHP scripts we need a web server (remember we mentioned above, "PHP is a server side language") that supports php; to name a few we have Apache, IIS. 




Brief overview of webservers Apache and IIS


Both Apache and IIS are designed  to work with wide range of languages. However, their execution environment is different. Apache provides cross-platform compatibility; can run on windows, Linux and many others. IIS, on the other hand, is specifically designed for Windows OS.

For you beginners, I will recommend downloading and installing  Xampp;  a freeware which is an all in one application comprising of Apache server, Mysql as database and supports PHP.

Another alternative would be to use a free hosting servicewhich provides pre installed Apache,mysql services.
 (You can try X10Hosting  or 000webhost hosting service. You will have to create an account first ) .


  Installing Xampp on vista/windows 7:

  1. Run the .exe application.Click ok.
  2. After installation, cmd.exe prompt will appear. Enter y (yes) or n (no) to customize the type of     installation you need.
  3. When done press 1, when you see the screen below: 



4.Now you shall be see the Xamp Control panel.
5. Check Apache->click ok.
6. Check Mysql->click ok.





7. Now to start Mysql and Apache service->click start for each.
8. If successful, Running highlighted in green shall appear next to Apache and Mysql (see screenshot below)





Let's get started(PHP syntax)

  •      Every php script starts with <?php  and ends with ?>.
  •      PHP codes go within the <?php  ?> syntax.
  •      To print/output a plaintext, we use echo keyword.
    •   For example echo 'a' will output a.
    • The text can be enclosed in double or single quote, either echo "a" or echo 'a'.
  •       To comment a line, we use // (for single line comment)  or /* comments */(for multiline comments)
  •       Each line of code ends with a semicolon (;)
  •       To run a php script locally, we use http://localhost/file.php.

   

 

A simple example:

  •     We will write a small script to test whether xampp was successfully installed.
  •     The script will print  "xampp was successfully installed ".


  1. Open notepadd++ or any text editor.
  2. Type in the code below:


               <?php
                    echo " xampp was successfully installed " ; //  use slash to comment a  single line comment 
                     /*  
                            to comment more than 1 line
                            start with slash star 
                           end with star slash!
                   */
            ?>



3. Save script as test.php in htdocs folder (go to c:/xampp/htdocs).

4.  Open your browser type "http://localhost/test.php".
      You shall see the output  'xampp was successfully installed' ( see screenshot below).