Friday, March 12, 2010

11:45 PM

we are going to create our PHP file that will process the data. When you submit your HTML form PHP automatically populates two superglobal arrays, $_GET and $_POST, with all the values sent as GET or POST data, respectively. Therefore, a form input called 'Name' that was sent via POST, would be stored as $_POST['Name'].

Copy and paste this code and save it as process.php in the same directory as form.html.
<?php
//Check whether the form has been submitted
if (array_key_exists('check_submit', $_POST)) {
   //Converts the new line characters (\n) in the text area into HTML line breaks (the <br /> tag)
   $_POST['Comments'] = nl2br($_POST['Comments']); 
   //Check whether a $_GET['Languages'] is set
   if ( isset($_POST['Colors']) ) { 
     $_POST['Colors'] = implode(', ', $_POST['Colors']); //Converts an array into a single string
   }

   //Let's now print out the received values in the browser
   echo "Your name: {$_POST['Name']}<br />";
   echo "Your password: {$_POST['Password']}<br />";
   echo "Your favourite season: {$_POST['Seasons']}<br /><br />";
   echo "Your comments:<br />{$_POST['Comments']}<br /><br />";
   echo "You are from: {$_POST['Country']}<br />";
   echo "Colors you chose: {$_POST['Colors']}<br />";
} else {
    echo "You can't see this page without submitting the form.";
}
?>
Let's give a little explanation. At the first line we check whether the form has been submitted and the php script has not been called directly. Next we convert the new line characters in the text area into HTML line breaks. Then we check whether a $_POST['Colors'] is set and if so we use implode() function to convert $_POST['Colors'] array into a single string. Finally, we print out all received values in the browser.
GET and POST

When defining the method to send information to the PHP script, you either use GET or POST. Both send variables across to a script, but they do so in different ways.
The GET method sends its variables in the web browsers URL, which makes it easy to see and possibly change the information that was sent. So this method should not be used when sending passwords or other sensitive information. It also should not be used for any actions that cause a change in the server, such as placing an order or updating a database. However, because the variables are displayed in the URL, it is possible to bookmark the page.
The GET method has a limit on the amount of information than can be sent. As a result, if you send long variables using GET, you are likely to lose large amounts of them.
The POST method sends its variables behind the scenes and has no limits on the amount of information to be sent. Because the variables are not displayed in the URL, it is not possible to bookmark the page.

0 comments: