unidox Posted August 5, 2008 Share Posted August 5, 2008 I have a select form that allows you to select multiple values, I was wondering how I would in php turn all of them into an array, and then I can use a foreach. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/118247-solved-multiple/ Share on other sites More sharing options...
vikramjeet.singla Posted August 5, 2008 Share Posted August 5, 2008 suppose you have these options in select male and female than you can use as mentioned below: <?php $a = array('male'=>'Male', 'female'=>'Female'); ?> <select name="abc"> <?php foreach($a AS $key=>$value){ echo "<option value=\"".$key."\">".$value."</option>"; } ?> </select> Quote Link to comment https://forums.phpfreaks.com/topic/118247-solved-multiple/#findComment-608546 Share on other sites More sharing options...
unidox Posted August 5, 2008 Author Share Posted August 5, 2008 No, I have a form like: <select name="names" multiple="multiple"> <option value="junk">Junk</option> <option value="trunk">Trunk</option> </select> If the user selects both values and then submits the form, how do I turn the values into an array than I can use foreach. Quote Link to comment https://forums.phpfreaks.com/topic/118247-solved-multiple/#findComment-608599 Share on other sites More sharing options...
.josh Posted August 5, 2008 Share Posted August 5, 2008 example: <?php // foreach loop foreach ($_POST['names'] as $key => $val) { echo "$key $val <br />"; } // echo individually echo $_POST['names'][0]; // echo "junk" echo $_POST['names'][1]; // echo "trunk" ?> <br /> <form method = 'post' action = ''> <!-- the select name needs to be an array --> <select name="names[]" multiple="multiple"> <option value="junk">Junk</option> <option value="trunk">Trunk</option> </select> <br /><input type = 'submit' value = 'submit'> </form> Quote Link to comment https://forums.phpfreaks.com/topic/118247-solved-multiple/#findComment-608608 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.