## Topic outline

• ### PHP Form Examples

• Scripts will interact with their clients using one of the two HTTP methods.

The methods are GET and POST , When a form is submitted using the GET method, its values are encoded directly in the query string portion of the URL .When a form is submitted using the POST method, its values will not be displayed the query string portion of the URL.

The $_GET Function The built-in$_GET function is used to collect values from a form sent with method="get"

Information sent from a form with the GET method is visible to everyone (it will be displayed in the browser's URL) and has limits on the amount of information to send (max. 100 characters)

This method should not be used when sending passwords or other sensitive information.

However, because the variables are displayed in the URL, it is possible to bookmark the page .The get method is not suitable for large variable values; the value cannot exceed 100 characters

The $_POST Function The built-in$_POST function is used to collect values from a form sent with method="post"

Information sent from a form with the POST method is invisible to others and has no limits on the amount of information to send

However, there is an 8 Mb max size for the POST method, by default (can be changed by setting the post_max_size in the php.ini file)

Example : 1

The following example illustrates to create simple form in PHP.Start WAMPServer,write the following code in Notepad++

Form1.html

<html>

<body>

/* form submitted using ‘get’ method, action specifies    next page which is to be loaded when button is clicked

<form action="welcome.php" method="get">

// textbox is to take user input     Name: <input type="text" name="fname" />

Age: <input type="text" name="age" />     // Submit button is to submit the value

<input type="submit" />

</form>

</body>

</html>

Create another notepad with name welcome.php in same folder as above c->xampp->htdocs->oursamplefoldername->welcome.php

welcome.php

<html>

<body>

// $_GET to receive the data sent from Form1.html Welcome <?php echo$_GET["fname"]; ?>.<br />

You are <?php echo $_GET["age"]; ?> years old! </body> </html> Example 2 : Another example with all controls used in PHP Form Form.html <html> <head> <title>Process the HTML form data with the POST method</title> head> <body> /* form submitted using ‘post’ method, action specifies next page which is to be loaded when button is clicked */ <form name="myform" action="process.php" method="POST"> // create an hidden textbox <input type="hidden" name="check_submit" value="1" /> // textbox is to take user input Name: <input type="text" name="Name" /><br /> Password: <input type="password" name="Password" maxlength="10" /><br /> // Use ‘select’ tag to display the various options Select something from the list: <select name="Seasons"> <option value="Spring" selected="selected">Spring</option> <option value="Summer">Summer</option> <option value="Autumn">Autumn</option> <option value="Winter">Winter</option> </select><br /><br /> Choose one: //This will create radio buttons <input type="radio" name="Country" value="USA" /> USA <input type="radio" name="Country" value="Canada" /> Canada <input type="radio" name="Country" value="Other" /> Other <br /> Choose the colors: //This will create checkbox <input type="checkbox" name="Colors[]" value="green" checked="checked" /> Green <input type="checkbox" name="Colors[]" value="yellow" /> Yellow <input type="checkbox" name="Colors[]" value="red" /> Red <input type="checkbox" name="Colors[]" value="gray" /> Gray <br /> // Submit button is to submit the value to next page <input type="submit" /> </form> </body> </html> Process.php <html> <body> <?php if (array_key_exists('check_submit',$_POST))

{      /*Converts the new line characters (\n) in the text rea 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 "You are from: {$_POST['Country']}<br />";

echo "Colors you chose: {\$_POST['Colors']}<br />";

}

else