multi select box

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
barnes529
Forum Newbie
Posts: 17
Joined: Thu Apr 19, 2007 4:18 am

multi select box

Post by barnes529 »

hi to all
i need small help in php.
how to store multi select box values in database.suppose one person prefers 2 or 3 cities in a country.during the normal process last selected value is stored in database.but i want to store all selected values in multiple select box.
thanks in advance..
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Are you curious as to how the information is posted or as to how you'd go about inserting it into the database?
Begby
Forum Regular
Posts: 575
Joined: Wed Dec 13, 2006 10:28 am

Post by Begby »

Perhaps you should start by writing in complete sentences, you know, with things like capitalization. Maybe you could throw in a couple spaces after periods for fun.

Better yet, maybe you should write a filter in PHP that will do this. You can type in your post into a form field, then it will convert your text to something more readable and then you can copy and paste it. This would be very good regular expression practice.

As for your question you can pass html form elements as arrays by naming the element with brackets like the following

Code: Select all


<select name="stuff[]" multiple>
 <option value='1'>stuff</option>
 <option value='2'>more stuff</option>
 <option value='3'>turkey</option>
</select>

Then in php

Code: Select all

// This is now an array
$stuff = $POST['stuff'] ;

foreach( $stuff as $value )
{
   echo $value ;  // Prints out the selected values
}
Post Reply