1 of 21

PHP UNIT-III

  • Working with Forms
  • Introducing Cookies

2 of 21

UNIT-III

Working with Forms

        • Creating Forms
        • Accessing form input
        • Saving state – hidden fields
        • File uploads

3 of 21

What is a form?

Forms are generally used to collect user inputs through text boxes, check boxes, radio buttons etc.

4 of 21

What is a form?

<form action=“myform.php” method="get">Name: <input type="text" name=“personname”> <br>E-mail: <input type="text" name="email"> <br><input type="submit">

</form>

  • When the user fills out the form above and clicks the submit button, the form data is sent for processing to a PHP file named “myform.php".
  • The form data is sent with the HTTP GET method.

5 of 21

GET and POST methods

<form action=“myform.php” method="get">Name: <input type="text" name=“personname”> <br>E-mail: <input type="text" name="email"> <br><input type="submit">

</form>

  • When the user fills out the form above and clicks the submit button, the form data is sent for processing to a PHP file named “myform.php".
  • The form data is sent with the GET method.

6 of 21

GET and POST methods

  • Both GET and POST create an array. This array holds key/value pairs, where keys are the names of the form controls and values are the input data from the user.

  • Both GET and POST are treated as $_GET and $_POST. These are superglobals, which means that they are always accessible, regardless of scope - and we can access them from any function, class or file.

7 of 21

GET and POST methods

  • $_GET is an array of variables passed to the current script via the URL parameters.

  • $_POST is an array of variables passed to the current script via the HTTP POST method.

8 of 21

When to use GET?

  • Information sent from a form with the GET method is visible to everyone (all variable names and values are displayed in the URL).
  • GET also has limits on the amount of information to send. The limitation is about 2000 characters.
  • GET may be used for sending non-sensitive data.

  • Note: GET should NEVER be used for sending passwords or other sensitive information!

9 of 21

When to use 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.

10 of 21

Getting form data

  • To access the value, use the $_POST['inputname'] array, where “inputname” is the name in the HTML element.
  • If the method is post, use “$_POST” . If the method is get, use $_GET.

11 of 21

Getting form data

<form action=“myform.php” method="get">  

Name: <input type="text" name=“personname"/>  

<input type="submit" value=“submit”/>  

</form> 

<?php  

$name=$_GET[“personname”];  

echo "Welcome, $name";  

?>  

 

12 of 21

Accessing form Input with User-Defined Arrays

  • We know how to gather information from HTML elements that submit a single value per element name, such as text fields, text areas, and radio buttons.

<input type="text" name="user">

<?php

echo “Welcome $_POST[user]";

?>

  • When working with elements like SELECT, it possible for the user to choose one or more items in a multiple SELECT list.

13 of 21

Accessing form Input with User-Defined Arrays

<html>

<body>

<form action=“take.php" method="POST">

Name: <input type="text" name="user">

Select Product: <select name="products[]" multiple>

<option value=“v1 ">Maruti</option>

<option value=“v2">Toyoyo</option>

<option value=“v3">TATA</option>

<option value=“v4">Hyundai</option>

</select>

<input type="submit" value="send“>

</form>

</body>

</html>

<html>

<body>

<?php

echo “Welcome $_POST[user]";

echo “Your choices are:“;

if (!empty($_POST[products]))

{

foreach ($_POST[products] as $value)

{

echo “$value";

}

}

?>

</body>

</html>

14 of 21

Combining HTML and PHP code on a single page

  • We  might want to include the form-parsing PHP code on the same page as a hard-coded HTML form.
  • It is useful if we need to present the same form to the user more than once.

What is PHP_SELF variable?

  • PHP_SELF is a variable that returns the current script being executed. This variable returns the name and path of the current file (from the root folder). We can use this variable in the action field of the FORM.

15 of 21

Combining HTML and PHP code on a single page

  • Example:

�echo $_SERVER['PHP_SELF'];

a) Suppose the php file is located at the address:� http://www.Myserver.com/form-action.php

  • In this case, PHP_SELF will contain: "/form-action.php"

b) Suppose the php file is located at the address:� http://www.Myserver.com/dir1/form-action.php

  • For this URL, PHP_SELF will be : "/dir1/form-action.php"

16 of 21

Combining HTML and PHP code on a single page

  • Example:

�<form  method="post" action="form-action.php" >

We can use the PHP_SELF variable instead of “form-action.php”. The code becomes:

<form  method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" >

17 of 21

Combining HTML and PHP code on a single page

<?php

if(isset($_POST['submit']))

{

    $name = $_POST['name'];

    echo “Name : <b> $name </b>";

    echo "<br>Use the following form again to enter a new name.";

}

?>

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">

<input type="text" name="name"><br>

  <input type="submit" name="submit" value="Submit"><br>

</form>

18 of 21

Saving state – Hidden fields

  • A hidden field behaves exactly the same as a text field, except that the user cannot see it, unless he views the HTML source of the document that contains it. 
  • It simply stores the text value specified in the value attribute.

<input type="hidden” id=“cid" name="custId" value=“37">

<input type="hidden" name="name"

value="<?php echo $_POST['name']; ?>">

19 of 21

Saving state – Hidden fields

  • They are used to transmit state information between different pages.

<form method="post" action="form2.php">

    <input type="text" name="name">

    <input type="text" name="email_address">

    <input type="submit" value="Go To Step 2">

</form>

20 of 21

Saving state – Hidden fields

form2.php

<form method="post“ action="form3.php">

<input type="radio“ group="membership_type" value="Free"> 

<input type="radio“ group="membership_type“ value="Normal">

<input type="checkbox“ name="terms_and_conditions">

<input type="hidden" name="name“ value="<?php echo $_POST['name']; ?>">

<input type="hidden" name="email_address“  value="<?php echo $_POST['email_address']; ?>">

<input type="submit" value="Go To Step 3">

</form>

21 of 21

Redirecting the user

  • Redirection in PHP can be done using the header() function.
  • To setup a simple redirect, simply create an index.php file in the directory you wish to redirect from with the following content:

< ?php header("Location: http://www.wbsite.com/"); ?>

  • Where 'http://www.redirect.to.url.com/' is the URL you wish the users to be redirected too.
  • This can also be a file

<?php header("Location: anotherDirectory/anotherFile.php"); ?>