Archive for October 2009

How to use Form Select Statement in PHP

Wednesday, October 14, 2009 · Posted in ,

Using a select statement in one page is commonly use by web programmers in building web applications and websites. The sample codes below illustrates on how to use and on how to simply code Form select statement.

HTML CODE:
<html>
<head>
<title>Title goes heretitle>
head>
<body>
<form action="" method="post">
<select name="day">
<option value="Monday" selected="selected">Mondayoption>
<option value="Tuesday">Tuesdayoption>
<option value="Wednesday">Wednesdayoption>
<option value="Thursday">Thursdayoption>
<option value="Friday">Fridayoption>
<option value="Saturday">Saturdayoption>
<option value="Sunday">Sundayoption>
select>
<br/>
<br/>
<input type="submit" name="btn_submit" value="submit" />
form>
body>
html>

As you can observe from the codes above that we did not specify the form action. So this means that if we will going to run the codes above the return would only on its self because of the reason that we did not specify the action in which the form will going to pass the value after the user submit the form. Also as you can observe inside the select statement we have and option which has a value of the complete days from Monday to Sunday, as you notice I put an attribute in the first option which is selected="selected" this shows that every time that the page is being refresh or open the first option will be the select item to be seen by the user.


PHP CODE:

<html>
<head>
<title>Title goes heretitle>
head>
<body>
<form action="" method="post">
<select name="day">
<option value="Monday" selected="selected">Mondayoption>
<option value="Tuesday">Tuesdayoption>
<option value="Wednesday">Wednesdayoption>
<option value="Thursday">Thursdayoption>
<option value="Friday">Fridayoption>
<option value="Saturday">Saturdayoption>
<option value="Sunday">Sundayoption>
select>
<br/>
<br/>
<input type="submit" name="btn_submit" value="submit" />
form>

if(isset($_POST['btn_submit']))
{
echo "Today is: ".$_POST['day'];
}
?>body>
html>
OUTPUT:


Lets assume that the user select the Wednesday, so that is why, the given sample output above shown the return value as Wednesday. Because after the use select the value Wednesday and submit the form, the PHP script will now be executed and display the value the being submitted by the form. You can also try this with your own. If there are some comments or questions about this article just CLICK THIS LINK to be redirected to the forum page.

Powered by Blogger.