www.webdeveloper.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 30

Thread: multi-select

  1. #1
    Join Date
    May 2003
    Posts
    225

    multi-select

    I made this short code, wondering if and how php gets all the values from a multi select:
    PHP Code:
    <html>
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>

    <body>
    <form name="form1" method="post" action="index.php?action=post">
      <p><select name="select" size="6" multiple>
          <option value="1" selected>1</option>
          <option value="2">2</option>
          <option value="3" selected>3</option>
          <option value="4">4</option>
          <option value="5" selected>5</option>
          <option value="6">6</option>
        </select>
      </p>
      <p>
        <input type="submit" name="Submit" value="Submit">
      </p>
    </form>
    <p>
      <?php if(@$HTTP_GET_VARS['action']=='post'){echo $HTTP_POST_VARS['select'];} ?></p>
    </body>
    </html>
    and found it only returned the last one selected. How can I make a script returning all of them as a list, like:

    is no. 1 selected? yes
    no. 2? no
    no 3? yes...

    how can I do this?

  2. #2
    Join Date
    Dec 2002
    Location
    High on life
    Posts
    10,104
    When you set up the select box, make it an array by going name[]. Then, in PHP, you can loop through the selected items and print them out. Like this:

    PHP Code:
    <form name="form1" method="post" action="<?PHP echo $_SERVER["PHP_SELF"]; ?>">
      <p><select name="myselect[]" size="6" multiple="multiple">
          <option value="1">1</option>
          <option value="2">2</option>
          <option value="3">3</option>
          <option value="4">4</option>
          <option value="5">5</option>
          <option value="6">6</option>
        </select>
      </p>
      <p>
        <input type="submit" name="submit" value="Submit">
      </p>
    </form>
    <p>
    <?php if(isset($_POST['submit'])) {
        foreach (
    $HTTP_POST_VARS['myselect'] as $value) {
            echo 
    $value;
        };

    ?>
    </p>

    Personal website http://www.ryanbrill.com/
    Business website: http://www.infinitywebdesign.com/
    TypeSpace http://www.typespace.org/

    I reject your reality and substitute it with my own!

  3. #3
    Join Date
    May 2003
    Posts
    225
    can you return, say if the 2nd on the list is selected or not?

  4. #4
    Join Date
    Dec 2002
    Location
    High on life
    Posts
    10,104
    Yes, like this:

    PHP Code:
    <?php if(isset($_POST['submit'])) {
        foreach (
    $HTTP_POST_VARS['myselect'] as $value) {
            if (
    $value == "2") {
                echo 
    "number 2 is selected";
            }
            else {
                echo 
    "number 2 is not selected";
            }
        };

    ?>

    Personal website http://www.ryanbrill.com/
    Business website: http://www.infinitywebdesign.com/
    TypeSpace http://www.typespace.org/

    I reject your reality and substitute it with my own!

  5. #5
    Join Date
    May 2003
    Posts
    225
    when I select 1 and 2, it says "number 2 is not selectednumber 2 is selected
    "
    All i want is a list, going all the way down:
    1 = yes
    2 = no
    3 = no
    4 = yes...

    how is this done?

  6. #6
    Join Date
    Dec 2002
    Location
    High on life
    Posts
    10,104
    Well, the problem is the fact that it only sends the ones that are selected. So, the only way (that I know of) would be to do it manually...

    Personal website http://www.ryanbrill.com/
    Business website: http://www.infinitywebdesign.com/
    TypeSpace http://www.typespace.org/

    I reject your reality and substitute it with my own!

  7. #7
    Join Date
    May 2003
    Posts
    225

    string

    I know how to do it in JS, but how do you do string commands in PHP? What is the command for indexof() ?

    If i can use that, than

    if($multi.indexof('1')!= -1){echo "yes";}else{echo "no!";}

    Right?

    how about somthing to that sort?

  8. #8
    Join Date
    Dec 2002
    Location
    High on life
    Posts
    10,104
    I believe the PHP equivalent of indexOf is strstr()

    Personal website http://www.ryanbrill.com/
    Business website: http://www.infinitywebdesign.com/
    TypeSpace http://www.typespace.org/

    I reject your reality and substitute it with my own!

  9. #9
    Join Date
    May 2003
    Posts
    225
    How do you use it?-
    err... I just relised you had a link...

    you mean strpos() (I did my homework )

    do you have an example?

  10. #10
    Join Date
    May 2003
    Posts
    225
    found my own(well, modified from php.net):
    PHP Code:
    <html>
    <head>
    </head>
    <body>
    <?php
    if(isset($HTTP_POST_VARS['s'])){
    $mystring =  @$HTTP_POST_VARS['s'];
    $findme   =  @$HTTP_POST_VARS['f'];
    $pos strpos($mystring$findme);

    // Note our use of ===.  Simply == would not work as expected
    // because the position of 'a' was the 0th (first) character.
    if ($pos === false) {
        echo 
    "The string '$findme' was not found in the string '$mystring'";
    } else {
        echo 
    "The string '$findme' was found in the string '$mystring'";
        echo 
    " and exists at position $pos";
    }
    }
    ?>
    <form name="form1" method="post" action="find.php">
      <p> 
        <input name="s" type="text" id="s">
        string<br>
        <input name="f" type="text" id="f">
        to find<br>
        <input type="submit" name="Submit" value="Submit">
      </p>
    </form>
    </body>
    </html>
    (finished extra credit )
    Last edited by diamonds; 06-30-2003 at 03:08 PM.

  11. #11
    Join Date
    Dec 2002
    Location
    High on life
    Posts
    10,104
    Originally posted by diamonds
    you mean strpos() (I did my homework )
    Hmm... not sure which would be technically closest to the javascript indexOf()
    From the javascript 1.4 manual:

    indexOf
    Returns the index within the calling String object of the first occurrence of the specified value, starting the search at fromIndex, or -1 if the value is not found.
    From php.net manual

    strstr -- Find first occurrence of a string
    From php.net manual

    strpos -- Find position of first occurrence of a string

    Personal website http://www.ryanbrill.com/
    Business website: http://www.infinitywebdesign.com/
    TypeSpace http://www.typespace.org/

    I reject your reality and substitute it with my own!

  12. #12
    Join Date
    May 2003
    Posts
    225
    Well, mabye you are correct, but I was figuring from the fact that they both return the position of a string.

    Now, how can I incorperate it into a full script? I'm havinf some troubles!

  13. #13
    Join Date
    Dec 2002
    Location
    High on life
    Posts
    10,104
    What exactly are you trying to do? From the looks of it, a regexp might be more appropriate anyway...

    Personal website http://www.ryanbrill.com/
    Business website: http://www.infinitywebdesign.com/
    TypeSpace http://www.typespace.org/

    I reject your reality and substitute it with my own!

  14. #14
    Join Date
    May 2003
    Posts
    225
    Well, now I'm trying to use the function to find if a value is returned in a muli select or not.

  15. #15
    Join Date
    Dec 2002
    Location
    High on life
    Posts
    10,104
    So you are just trying to see if they selected an option or not? Try something like this:

    PHP Code:
    <?PHP
    if (isset($_POST["submit"])) {
        if (isset(
    $_POST["myselect"])) {
            echo 
    "Options were selected";
        }
        else {
            echo 
    "No options were selected";
        }
    }
    ?>

    <html>
    <head>
    </head>

    <body>
    <form action="<?PHP echo $_SERVER["PHP_SELF"]; ?>" method="post">
    <!-- The brackets in the select name are important, once we get to the PHP part... It tells the script that it is an array -->
    <select name="myselect[]" multiple="multiple"> 
    <option value="1">1 
    <option value="2">2 
    <option value="3">3 
    </select>
    <input type="submit" name="submit" value="submit">
    </form>
    </body>
    </html>

    Personal website http://www.ryanbrill.com/
    Business website: http://www.infinitywebdesign.com/
    TypeSpace http://www.typespace.org/

    I reject your reality and substitute it with my own!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles