Sei sulla pagina 1di 11

Working With Form

Working With Form


PHP is really good at handling data submitted
to the server in HTML forms.
HTML forms allow users to enter data into a
web page.
On the server, we can use PHP page along the
form collection to handle information set.

Getting Form Values


<html><head><title>Your Favourites</title></head><body>

fav.html

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


Please enter your name :<input type="text" name="username" size="45"><p>
Please select your favourite colour : <input type="radio" name="colour" value="yellow"> Yellow
<input type="radio" name="colour" value="magenta"> Magenta
<input type="radio" name="colour" value="green"> Green <p>
Please select your favourite food :
<input type="checkbox" name="food1" value="Laksa"> Laksa
<input type="checkbox" name="food2" value="Curry Mee"> Curry Mee
<input type="checkbox" name="food3" value="Chicken Chop"> Chicken Chop <p>
<input type="submit" value=" Submit This Form ">
<input type="reset" value=" Clear ">
</form></body></html>

Getting Form Values

Displaying Submitted Values


fav.php

<html><head><title>Your Submission</title></head><body>
<?php
$username=$_POST["username"];
if($username != null) { echo "Thank you for your selection, $username ";}

if(isset($_POST["colour"])) // isset() function checks to see if the form has submitted using the POST method
{ $colour=$_POST["colour"];
if($colour == "yellow")

echo"<body bgcolor='yellow'>";

elseif($colour == "magenta")

echo"<body bgcolor='magenta'>";

else

echo"<body bgcolor='green'>"; }

echo"<p> Hope you really enjoy --> ";


if(isset($_POST["food1"]))

{ $food1 = $_POST['food1'];

echo" $food1"; }

if(isset($_POST["food2"]))

{ $food2 = $_POST['food2'];

echo" $food2"; }

if(isset($_POST["food3"]))

{ $food3 = $_POST['food3'];

echo" $food3"; }

?>

</body></html>

Displaying Submitted Values

Manipulation Submitted Values


PHP can do much more than simply display values
submitted from a HTML form.
The submitted data which stored in PHP variables
can be manipulated by the script, just like other
variables.

Manipulation Submitted Values


<html><head><title>Calculation Form</title></head>

calc.html

<body>
<form action="calc.php" method="post">
Value 1: <input type="text" name="val1" size=5>&nbsp&nbsp&nbsp&nbsp
Value 2: <input type="text" name="val2" size=5> <p>
<h4>Calculation:</h4>
<input type="radio" name="calc" value="add">Add
<input type="radio" name="calc" value="sub">Subtract
<input type="radio" name="calc" value="mul">Multiply
<input type="radio" name="calc" value="div">Divide <p>
<input type="submit" value="Calculate">
<input type="reset" value="Clear">
</form>
</body></html>

Manipulation Submitted Values

Displaying Submitted Values


<html><head><title>Calculation Result</title></head><body>
<?php

calc.php

$val1=$_POST["val1"];
$val2=$_POST["val2"];
if(is_numeric($val1) && is_numeric($val2))
{

if(isset($_POST["calc"]))
{

$calc=$_POST["calc"];
switch($calc)
{

case "add" : $result = $val1 + $val2; break;


case "sub" : $result = $val1 - $val2; break;
case "mul" : $result = $val1 * $val2; break;
case "div" : $result = $val1 / $val2; break;

echo("<h2>Calculation result: $result </h2>");


}

else {echo ("Invalid entry - please retry"); }


?> </body></html>

Displaying Submitted Values

Potrebbero piacerti anche