PHP UNIT-III
UNIT-III
Working with Forms
What is a form?
Forms are generally used to collect user inputs through text boxes, check boxes, radio buttons etc.
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>
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>
GET and POST methods
GET and POST methods
When to use GET?
When to use POST?
Getting form data
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";
?>
Accessing form Input with User-Defined Arrays
<input type="text" name="user">
<?php
echo “Welcome $_POST[user]";
?>
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>
�
Combining HTML and PHP code on a single page
What is PHP_SELF variable?
Combining HTML and PHP code on a single page
�echo $_SERVER['PHP_SELF'];
a) Suppose the php file is located at the address:� http://www.Myserver.com/form-action.php
b) Suppose the php file is located at the address:� http://www.Myserver.com/dir1/form-action.php
Combining HTML and PHP code on a single page
�<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']; ?>" >
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>
Saving state – Hidden fields
<input type="hidden” id=“cid" name="custId" value=“37">
<input type="hidden" name="name"
value="<?php echo $_POST['name']; ?>">
Saving state – Hidden fields
<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>
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>
Redirecting the user
< ?php header("Location: http://www.wbsite.com/"); ?>
<?php header("Location: anotherDirectory/anotherFile.php"); ?>