CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Apr 2003
    Location
    kathmandu, nepal
    Posts
    1,570

    Getting selected values in multi select comobo box through PHP

    Hi,

    I have the following HTML code right now.

    Code:
    <td width="20%"><SELECT NAME="cfg_input_global_report_types" MULTIPLE >
    	<OPTION VALUE="BU" >Option1
    	<OPTION VALUE="BP" >Option2
    	<OPTION VALUE="BC" >Option3
    	<OPTION VALUE="BL" >Option4
    	<OPTION VALUE="BCC">Option5
    	</SELECT></td>
    The form is submitted using POST method

    I know how to use the $_POST php function to get the "cfg_input_global_report_types" variable

    Code:
    $_POST["cfg_input_global_report_types"]
    But I don't know how the selected variables are formatted in the $_POST["cfg_input_global_report_types"] variable. If anybody could show me how the selected values are formated in the above variable it would be great.

    Or if this method doesn't work at all or if there is a more easy way, how can I get the selected values in my multiple select combo box.

    Thanx in advance.
    If there is no love sun won't shine

  2. #2
    Join Date
    May 2002
    Posts
    10,943

    Re: Getting selected values in multi select comobo box through PHP

    The code you posted will grab the value of the <SELECT> that is selected at the time of form submission. Are you attempting to get all the <OPTION> tags values? Is so, you can't do it without making a hidden <DIV> and having JavaScript put those in there for you.
    PHP Code:
    $_POST["cfg_input_global_report_types"
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  3. #3
    Join Date
    Apr 2003
    Location
    kathmandu, nepal
    Posts
    1,570

    Re: Getting selected values in multi select comobo box through PHP

    Quote Originally Posted by PeejAvery
    The code you posted will grab the value of the <SELECT> that is selected at the time of form submission. Are you attempting to get all the <OPTION> tags values?
    Hi PeejAvery, I don't need to get all the <OPTION> tag values. I simply want to get the values that is selected by the user at the time of form submission.

    With simple select it was not a problem. I can simply use the $_POST with the select's name. But with multiple select there could be several values that maybe selected by the user when submitting the form.

    So my question is:

    How the multiple selected values will be inside the $_POST["cfg_input_global_report_types"]. Will they be comma separated or will it be an array?
    If there is no love sun won't shine

  4. #4
    Join Date
    May 2004
    Location
    MN / NJ, United States
    Posts
    768

    Re: Getting selected values in multi select comobo box through PHP

    I wouldn't know off hand? But couldn't you just create a simple script and see what the output is ... ?
    *9-11-01* Never Forget; Never Forgive; Never Relent!
    "If we ever forget that we're one nation under God, then we will be a nation gone under." - Ronald Reagan

  5. #5
    Join Date
    Apr 2003
    Location
    kathmandu, nepal
    Posts
    1,570

    Re: Getting selected values in multi select comobo box through PHP

    Quote Originally Posted by Dr. Script
    I wouldn't know off hand? But couldn't you just create a simple script and see what the output is ... ?
    Thanx for the suggestion. I will try it. I will echo it.

    Till now I have been filling the values directly to my MySQL database, and the corresponding field comes to be empty.
    If there is no love sun won't shine

  6. #6
    Join Date
    Apr 2003
    Location
    kathmandu, nepal
    Posts
    1,570

    Re: Getting selected values in multi select comobo box through PHP

    I find this strange:

    When I select 3 out of 5 possible values, the echo output of the variable is the last item that has been selected. The count php function returns 1 idicating that it is not an array. Where are the other selected values. How can I get it. Googled the entire net but couldn't find a solution.
    If there is no love sun won't shine

  7. #7
    Join Date
    May 2002
    Posts
    10,943

    Re: Getting selected values in multi select comobo box through PHP

    Wait, you don't have to do anything if it is a multiple select box. The PHP will process it as an array.

    You have to make the <SELECT> an array.

    Code:
    <?php
    if(isset($_POST['selectbox'])){
      print_r($_POST['selectbox']);
      exit;
    }
    ?>
    
    <form action="" method="post">
      <select name="selectbox[]" multiple>
        <option value="a">a</option>
        <option value="b">b</option>
        <option value="c">c</option>
        <option value="d">d</option>
        <option value="e">e</option>
        <option value="f">f</option>
        <option value="g">g</option>
        <option value="h">h</option>
        <option value="i">i</option>
        <option value="j">j</option>
        <option value="k">k</option>
      </select>
      <input type="submit" value="Submit">
    </form>
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  8. #8
    Join Date
    Apr 2003
    Location
    kathmandu, nepal
    Posts
    1,570

    Re: Getting selected values in multi select comobo box through PHP

    Splendid! PeejAvery Thanx a lot. Searched the net for at least 2 hours.
    If there is no love sun won't shine

  9. #9
    Join Date
    May 2002
    Posts
    10,943

    Re: Getting selected values in multi select comobo box through PHP

    You're welcome. I haven't used multiple <select> tags for years. Good luck with the rest!
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  10. #10
    Join Date
    Sep 2006
    Location
    Philippines
    Posts
    10

    Re: Getting selected values in multi select comobo box through PHP

    Set your form variable as array.

    <select name="var_name[]">

    <option value="1">1</option>
    .
    .
    </select>

    <?php
    print_r($_post['var_name']);
    ?>

  11. #11
    Join Date
    May 2002
    Posts
    10,943

    Re: Getting selected values in multi select comobo box through PHP

    @nes2rmp. Please read the whole thread before posting. The answer has already been provided and the problem solved. We don't want to flood the forum with useless posts.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  12. #12
    Join Date
    Sep 2006
    Location
    Philippines
    Posts
    10

    Wink Re: Getting selected values in multi select comobo box through PHP

    Thanks for the concern.

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

Featured