Results 1 to 5 of 5

Thread: Select Multiple Options

  1. #1
    Join Date
    Sep 2006
    Posts
    20
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Select Multiple Options

    I have a webpage I have made, to filter a list of emails. Currently it is set up to select by Branch and/or Group, and Branch and Group are drop downs where you can pick one. I would like to change it so that you can pick multiple ones, so I switched the page over so that it is a multiple select, but when it actually runs the query, it only outputs based on one of them.

    So the url result shows: "choice=request&search=03&search=04&search2=Q" and the list is only showing the search=04 people, it is ignoring the first search= part.

    I am not at all sure how to go about fixing this, or if I am even going about this the right way. I would appreciate any pointers. Thank you.


    Here is some of the relevant code:

    PHP Code:
    <FORM METHOD="get" ACTION="emails2.php">

    <
    INPUT TYPE=hidden NAME=choice VALUE="request">
    <
    form name="form1" method="get" action="emails2.php">
    Select a Branch
    <
    select multiple name="search" size=16>
    <
    option value="">All Branches</option>
    <
    option value="03">03 Portland</option>
    ...list 
    of options...
    </
    select>
    </
    td>
    <
    td>
    Select a Mail Group:
    <
    select multiple name="search2" size=21>
    <
    option value="">All Groups</option>
    <
    option value="A">Corporate Administration</option>
    ...list 
    of options...
    </
    select>
    </
    td></tr>
    </
    table>
    <
    INPUT TYPE="submit" VALUE="Search!" class="btn">
    </
    FORM
    PHP Code:
    if ($choice == "request")
    {
    echo 
    "All Selected Emails";
    $result 
    mysql_query("SELECT * FROM emails
         WHERE (Branch LIKE \"%
    $search%\" OR Branch = 'ALL') 
         AND (Mailgroup LIKE \"%
    $search2%\") 
         ORDER BY Name"
    );
    }; 

  2. #2
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by oriecat View Post
    So the url result shows: "choice=request&search=03&search=04&search2=Q" and the list is only showing the search=04 people, it is ignoring the first search= part.
    As PHP reads the query string, it adds name/value pairs to the $_GET superglobal. If a name occurs twice, the existing value is overridden.

    To get around this, you can add a pair of square brackets after the name. The name will remain the same, but PHP will create an array into which it will insert each value.

    So, in the markup,

    HTML Code:
    <select name="search" ...>
    becomes:

    HTML Code:
    <select name="search[]" ...>
    The data will still be available from $_GET['search'], but the value won't evaluate to a string, it will be an array.

    SELECT * FROM emails
    &#160;&#160;&#160;&#160;WHERE (Branch LIKE \"%$search%\" OR Branch = 'ALL')
    &#160;&#160;&#160;&#160;AND (Mailgroup LIKE \"%$search2%\")
    &#160;&#160;&#160;&#160;ORDER BY Name
    That query doesn't seem capable of handling multiple search terms. Even if the value of $search was a long string of options - and it won't be - it wouldn't permit searching on each space-separated value within it.

    Hope that helps,
    Mike

  3. #3
    Join Date
    Aug 2007
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Cheers for that Mike, that worked like a charm.

  4. #4
    Join Date
    Aug 2007
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    what if I have an invalid query?

  5. #5
    Join Date
    May 2008
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Select Multiple Options

    you can try this. Hopefully solved the problem.



    <html>
    <head>
    <script type="text/javascript">
    function formAction(){
    var x=document.getElementById("aSelectControl")
    x.multiple=true
    }
    </script>
    </head>

    <body>
    <form>
    <select id="aSelectControl" size="3">
    <option>Java</option>
    <option>PHP</option>
    <option>Perl</option>
    </select>
    <input type="button" onclick="formAction()" value="Set to

    Select multiple">
    </form>
    </body>
    </html>
    Regards:
    Hank Freid

    ___________________
    the developer hank freid redefines the chic hotel experience
    Last edited by Hank Freid; 05-19-2008 at 05:39 AM. Reason: for correction

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •