PHP Code Snippets Episode 2 – Use An Array To fill a Option Box


In this post I am going to show you how to take the values from an array and use them, in a form option box.

First of all we are going to make a simple array..

For this example i will just make a basic array, however you could use a mysql_fetch_array() to read data from a database to make your array then continue with step 2.

I will show you the basics of this in my next tut.


As you can see this will give you all the options from the array into your option choices.

I hope this little snippet will help.

To view the next tutorial where i use a database to get information please click here

Leave a Response

2 Responses

I would like to know about that option . How can i do to get the value of option and i want to check. for example if i choose any of one in option,I want to see it display about i choose for the above option.But the data is in database.Please respone to me.

HI many thanks,

I am translating your question to ask that if you select an option it will automatically display the database information to the page below.
We can use 2 methods for this, the first I will try to show you below,
is a simple onChange to submit the form, which will then call a simple php page with the same options.
Im guessing you will also want the option box to be self filled with data from the database also.
If you read my next article i will hopefully provide you with the information you may require..

the second is to use an ajax call to insert the data onto the page. This can be just as easy as the first method
and maybe neater but for the demostration I have chosen method 1.

Please read the next article.