There are two ways of creating a form. You can use HTML to create a form or you can use PHP to do the same.
The following steps are needed to create a form:
<form action="script_name" method="GET|POST">
<input type="text" name"" size="" value="" maxlength="" />
<input type="checkbox" name="" value="" />
<input type="radio" name="" value="" />
<input type="password" name="" />
<input type="hidden" name="" />
<select name=""><option></option></select>
<textarea name="" rows="" cols=""></textarea>
<input type="submit" value="submit">
</form>
This is a form using the GET method.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="EN" lang="EN"> <head> <title>PHP</title> <meta Name="Author" Content="Hann So"> </head> <body> <p> <form action="example_form_process.php" method="GET"><br /> Name: <input type="text" name="name"><br /> Choose a place: <input type="checkbox" name="place1" value="New York" />New York <input type="checkbox" name="place2" value="San Francisco" />San Francisco <input type="checkbox" name="place3" value="LA" />LA <br /> What do you like: <input type="radio" name="choice" value="condo" />condo <input type="radio" name="choice" value="house" />house <input type="radio" name="choice" value="rent" />rent <br /> Vacation spot:<select name="vacation"> <option value="paris" />Paris</option> <option value="london" />London</option> <option value="rome" />Rome</option> </select> <br /> Comments: <textarea name="comments" rows="5" cols="50"></textarea><br /> <input type="submit" value="submit"> <input type="reset" value="Clear"> </form> </p> </body> </html> |
This is the PHP script to handle the form.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="EN" lang="EN"> <head> <title>PHP</title> <meta Name="Author" Content="Hann So"> </head> <body> <p> <?php Here is the data from the form: </p> <p> <?php // get the ndata from $_REQUEST foreach($_REQUEST as $key =>$value) { if (is_array($value)) { foreach ($value as $item) { echo $key, " = ", $items, "<br />"; } } else { echo $key, " = ", $value, "<br />"; } } ?> </p> </body> </html> |
This is a form using the POST method.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="EN" lang="EN"> <head> <title>PHP</title> <meta Name="Author" Content="Hann So"> </head> <body> <p> <form action="example_form_process.php" method="POST"><br /> Name: <input type="text" name="name"><br /> Choose a place: <input type="checkbox" name="place1" value="New York" />New York <input type="checkbox" name="place2" value="San Francisco" />San Francisco <input type="checkbox" name="place3" value="LA" />LA <br /> What do you like: <input type="radio" name="choice" value="condo" />condo <input type="radio" name="choice" value="house" />house <input type="radio" name="choice" value="rent" />rent <br /> Vacation spot:<select name="vacation"> <option value="paris" />Paris</option> <option value="london" />London</option> <option value="rome" />Rome</option> </select> <br /> Comments: <textarea name="comments" rows="5" cols="50"></textarea><br /> <input type="submit" value="submit"> <input type="reset" value="Clear"> </form> </p> </body> </html> |
PHP has access to the the type of request being used in the $_SERVER['REQUEST_METHOD'] special superglobal array. To check which methoid is used, the following conditional can be included in your PHP script:
if ($_SERVER['REQUEST_METHOD']=='GET') {......}
if ($_SERVER['REQUEST_METHOD']=='POST') {......}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="EN" lang="EN"> <head> <title>PHP</title> <meta Name="Author" Content="Hann So"> </head> <body> <p> <?php if ($_SERVER['REQUEST_METHOD'] == 'GET') { ?> <form action = "<?php echo $_SERVER['SCRIPT_NAME'] ?>" method="post"> Name: <input type="text" name="name"><br /> Choose a place: <input type="checkbox" name="place1" value="New York" />New York <input type="checkbox" name="place2" value="San Francisco" />San Francisco <input type="checkbox" name="place3" value="LA" />LA <br /> What do you like: <input type="radio" name="choice" value="condo" />condo <input type="radio" name="choice" value="house" />house <input type="radio" name="choice" value="rent" />rent <br /> Vacation spot:<select name="vacation"> <option value="paris" />Paris</option> <option value="london" />London</option> <option value="rome" />Rome</option> </select> <br /> Comments: <textarea name="comments" rows="5" cols="50"></textarea><br /> <input type="submit" value="submit"> <input type="reset" value="Clear"> </form> <?php } else { // get the ndata from $_REQUEST foreach($_REQUEST as $key =>$value) { if (is_array($value)) { foreach ($value as $item) { echo $key, " = ", $items, "<br />"; } } else { echo $key, " = ", $value, "<br />"; } } } ?> </p> </body> </html> |