Results 1 to 4 of 4

Thread: how to pull all values from a select box

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2006
    Posts
    147

    how to pull all values from a select box

    ok i have this

    <select name="testCol[]" id="endresult" size=20>
    <option value=1>1</option>
    <option value=2>2</option>
    <option value=3>3</option>
    <option value=4>4</option>
    </select>


    now i need to pull all these values after someone hits the submit box, i tried this but it doesnt work

    foreach($_POST['testCol'] as $key => $value){
    echo $key." ---- ". $value ."<br>";
    }


    what should i do?

  2. #2
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: how to pull all values from a select box

    you can't get all of those values with a select box, unless the user picks all of them, which is impossible. making that <select> an array just makes it possible for you to have multiple <select>s that will share the same array when you submit the form.

    to do what you want to do, you would probably have to do something like:
    Code:
    <select name="testCol">
      <option value="1">1</option>
      <option value="2">2</option>
      <option value="3">3</option>
    </select>
    <input type="hidden" name="arrCol[]" value="1" />
    <input type="hidden" name="arrCol[]" value="2" />
    <input type="hidden" name="arrCol[]" value="3" />
    this way, the user's selection ($_POST['testCol'] as a string) and all of the possible values ($_POST['arrCol'] as an array) are all passed. $_POST['arrCol']'s keys will be generated automatically (although if you want specific keys you could do that, too), but all of the possible values will be the same.

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: how to pull all values from a select box

    Sure is possible, but the user would actually have to be able to select multiple.
    Code:
    <select name="testCol[]" multiple="multiple">
    ...
    </select>
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  4. #4
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: how to pull all values from a select box

    Quote Originally Posted by kows
    you can't get all of those values with a select box, unless the user picks all of them, which is impossible. making that <select> an array just makes it possible for you to have multiple <select>s that will share the same array when you submit the form.

    to do what you want to do, you would probably have to do something like:
    Code:
    <select name="testCol">
      <option value="1">1</option>
      <option value="2">2</option>
      <option value="3">3</option>
    </select>
    <input type="hidden" name="arrCol[]" value="1" />
    <input type="hidden" name="arrCol[]" value="2" />
    <input type="hidden" name="arrCol[]" value="3" />
    this way, the user's selection ($_POST['testCol'] as a string) and all of the possible values ($_POST['arrCol'] as an array) are all passed. $_POST['arrCol']'s keys will be generated automatically (although if you want specific keys you could do that, too), but all of the possible values will be the same.
    I owe you a +ve -- woops

    Unless you have it as selected multiple and the user has selected everything, only those selected will appear in your array. Otherwise, you can use kows method. The best way, rather than sending the data to the user and then resending it in the form is to store it server side in a session.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

Posting Permissions

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



Click Here to Expand Forum to Full Width