Reading Data in a form: using list box, lesson -6-…

October 3
I

will talk in this lesson about reading data in a form using list box. List box is also a common HTML control, but it take a little special handling. It allows users to select multiple choices from a list and send the data together. I will show you how, Let’s begin…

 

Step *1: open xammp control panal, open your browser and open the editor. In htdocs folder create another folder and name it (listbox). Inside this folder you will save the files we will create in this lesson. In this folder as usual, we will have two files listbox.html and phplistbox.php.

 

Step *2: using the editor, create an HTML file, name it listbox.html, type this code in it and save.


<html>

<head>

<title>entering data in a form</title>

</head>

<body>

<h1>

entering data with list box

</h1>

select your favorites sport:

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

<select name="sport[]" multiple>

<option> tennis </option>

<option> soccer </option>

<option> basketball </option>

<option> jogging </option>

</select>

<br/>

<br/>

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

</form>

</body>

</html>

As you can see the form attributes are the same as we used to use except the “select” attribute, Therefore I won’t explain them any more and you can check the earlier lessons if you forgot how they work.

 

We use “select” attribute to create a list box, give it a name using the “name” attribute and to allow the user to select a multiple-selection control we will add the “[]” with the name to tell the PHP that this is an array. Not a single- selection control.

After that I will use the “option” element to add the choices in list box.

 

Step *3: Create a PHP file and name it phplistbox.php. Type this code in it and save.


<html>

<head>

<title>reading data from a form</title>

</head>

<body>

<h1>

reading data from list box

</h1>

your favorites sport is :

<br/>

<?php

foreach ($_REQUEST["sport"] as $yrsport) {

echo $yrsport. "<br/>";

}

?>

</body>

</html>

Now, we reached reading data in a form sent by list box. As usual, we will do  that by echoing $_request ["sport[]” to the browser. But because we have an array this time, we should use the “foreach” loop to echo all the selections.

 

This lesson is finished; wait for me next week with reading data in a form using password control, seeyas.

 


StumbleUpon.com Add us to your digg favorites Add us to your delicious favorites add us to your facebook favorites follow us on twitter Add us to your technorati favorites Add us to your wordpress favorites Add us to your reddit favorites Add us to livejournal favorites

Leave a Reply