Board index   FAQ   Search  
Register  Login
Board index PHP PHP General

Multi-selectbox not working as I hoped (solved)

General discussions related to php

Moderators: macek, egami, gesf

Multi-selectbox not working as I hoped (solved)

Postby Tosca » Tue Jul 10, 2012 2:16 am

Hi everyone

I have little experience of web development and don't know if what I am trying to do is possible, so here goes.

I have found some code that displays a multi-selectbox and have modified it slightly to use GET rather than POST so I can see what data is being passed in the address bar. Here's the code:

Code: Select all
<?php
    if(!isset($_GET['submit']))        # If this form has not been submitted
        $chosen = array(3, 6, 2, 7);    # Use the default
    else                                # Otherwise
        $chosen = $_GET['colors'];     # Use what has been submitted

    function showOptionsDrop($array, $active, $echo=true){
        $string = '';

        foreach($array as $k => $v){
            if(is_array($active))
                $s = (in_array($k, $active))? ' selected="selected"' : '';
            else
                $s = ($active == $k)? ' selected="selected"' : '';
            $string .= '<option value="'.$k.'"'.$s.'>'.$v.'</option>'."\n";
        }

        if($echo)   echo $string;
        else        return $string;
    }

    $colors = array('red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet', 'pink', 'black', 'white', 'silver', 'gold');

?>
<form name="colorform" action="" method="get">
    <select name="colors[]" multiple="multiple" size="15">
        <?php echo showOptionsDrop($colors, $chosen, true); ?>
    </select><br />
    <input type="submit" name="submit" value="submit" />
</form>



When I select one or more colours (yes, I am in the UK!) and click the Submit button, the address bar shows the index number(s) of the colour(s), rather than the colours themselves:

http://127.0.0.1/test.php?colors%5B%5D= ... mit=submit

My aim is to have a user select the colours and have them displayed on the page (so he can confirm the selection) and then submit them to a second page via another button.

I have tried IE8, Chrome and Firefox. I have xampp (with PHP version 5.3.8). I am not sure if what I am trying to do is impossible or if I am doing something wrong.

It is possible to retrieve that actual colours, rather than their index?

I have been pulling my hair out for ages and am going around in circles!

Thank you for your time (and patience).
Last edited by Tosca on Wed Jul 11, 2012 9:19 am, edited 1 time in total.
Tosca
New php-forum User
New php-forum User
 
Posts: 6
Joined: Tue Jul 10, 2012 1:44 am

Re: Multi-selectbox not working as I hoped

Postby Nullsig » Wed Jul 11, 2012 7:30 am

Use:
Code: Select all
$string .= '<option value="'.$v.'"'.$s.'>'.$v.'</option>'."\n";


Or:
Code: Select all
$string .= '<option '.$s.'>'.$v.'</option>'."\n";


When you use $k => $v notation it takes each element in the array and divides it into index($k) => value($v).

If you do not want to use the index to refer to the values in the array you can either use the $v variable in the value of the option as I did in the first example. Alternatively you can use the option tag without the value attribute which then causes the form to post string between the option tag as the value of the argument, as I have shown in the second example.
User avatar
Nullsig
php-forum Fan User
php-forum Fan User
 
Posts: 981
Joined: Thu Feb 17, 2011 6:52 am
Location: Racine, WI

Re: Multi-selectbox not working as I hoped

Postby Tosca » Wed Jul 11, 2012 9:19 am

I'm grateful Nullsig for your suggestions and the explanation about what's happening. I have seen the => syntax and guess I need to do a bit more reading about this as well as arrays in general!
Tosca
New php-forum User
New php-forum User
 
Posts: 6
Joined: Tue Jul 10, 2012 1:44 am


Return to PHP General

Who is online

Users browsing this forum: Bing [Bot] and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.