#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    204
    Rep Power
    3

    Multiple Select boxes ( $_POST )


    Hi! I have multiple select boxes ( for a sort of survey ) and i want to retrieve all selected items in the $_POST.. How would i do this?


    the select boxes are names like this
    <select name="antwoord0">
    <select name="antwoord1">
    <select name="antwoord2">
    ...

    Thank you!
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,542
    Rep Power
    595
    PHP Code:
    $selected0=$_POST['antwoord0'];
    $selected1=$_POST['antwoord1'];
    $selected2=$_POST['antwoord2']; 
    Last edited by gw1500se; October 29th, 2012 at 01:54 PM.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    204
    Rep Power
    3
    Originally Posted by gw1500se
    PHP Code:
    $selected0=$_POST['antwoord0'];
    $selected1=$_POST['antwoord1'];
    $selected2=$_POST['antwoord2']; 
    Thanks! Problem is the number of questions will be dynamic, so is it possible to use a loop to get all the results? thanks.

    PHP Code:
    $selected $count $_POST['antwoord'] . $count;  ? 
    ?
  6. #4
  7. Confused badger
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Mar 2009
    Location
    West Yorkshire
    Posts
    1,115
    Rep Power
    487
    Originally Posted by notflip
    Thanks! Problem is the number of questions will be dynamic, so is it possible to use a loop to get all the results? thanks.

    PHP Code:
    $selected $count $_POST['antwoord'] . $count;  ? 
    ?
    YES!

    Name your select elements like so:-
    PHP Code:
    echo "<select name='selectbox[]'>";
    echo 
    "<option value=1>1</option>";
    echo 
    "<option value=2>2</option>";
    echo 
    "<option value=3>3</option>";
    echo 
    "</select>";
    echo 
    "<select name='selectbox[]'>";
    echo 
    "<option value=1>1</option>";
    echo 
    "<option value=2>2</option>";
    echo 
    "<option value=3>3</option>";
    echo 
    "</select>";
    echo 
    "<select name='selectbox[]'>";
    echo 
    "<option value=1>1</option>";
    echo 
    "<option value=2>2</option>";
    echo 
    "<option value=3>3</option>";
    echo 
    "</select>"
    And then in you processing page, grab the data with

    $select_contents = $_POST['selectbox'];

    $select_contents then has an array of the selected options which you can use a foreach or for to iterate through.

    Comments on this post

    • notflip agrees : Solution! thanks
    "For if leisure and security were enjoyed by all alike, the great mass of human beings who are normally stupefied by poverty would become literate and would learn to think for themselves; and when once they had done this, they would sooner or later realise that the privileged minority had no function and they would sweep it away"
    - George Orwell, 1984
  8. #5
  9. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    204
    Rep Power
    3
    It's working thank you very much!
  10. #6
  11. Confused badger
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Mar 2009
    Location
    West Yorkshire
    Posts
    1,115
    Rep Power
    487
    What do you see if you print_r($_POST['antwoord']); ?
    "For if leisure and security were enjoyed by all alike, the great mass of human beings who are normally stupefied by poverty would become literate and would learn to think for themselves; and when once they had done this, they would sooner or later realise that the privileged minority had no function and they would sweep it away"
    - George Orwell, 1984
  12. #7
  13. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    204
    Rep Power
    3
    Originally Posted by badger_fruit
    What do you see if you print_r($_POST['antwoord']); ?
    It was my fault! working now! thanks a lot didn't know getting values from multiple selects is so easy. I used to make a whole for loop structure
  14. #8
  15. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    204
    Rep Power
    3
    One more question! is it possible to increment the key of the array?

    PHP Code:
        foreach ($antwoorden as $key => $value){
            echo 
    $key": " .$value "|";
        } 
    this would start at 0, but in my database the value of the first row is 1 so to insert this how would i increment this key value+1 ? i tried but i get an error. thanks!
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,542
    Rep Power
    595
    That does not make sense. The database does not determine how PHP stores data in arrays. Row 0 is the first row returned by the query. If the first row returned is not what you want then you need to re-examine your query.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  18. #10
  19. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    204
    Rep Power
    3
    Originally Posted by gw1500se
    That does not make sense. The database does not determine how PHP stores data in arrays. Row 0 is the first row returned by the query. If the first row returned is not what you want then you need to re-examine your query.
    I'll be more clear I have a field questions_id and answers_id.. the questions_id starts at 1 , 2 , 3 .. and

    for question one the answer is perhaps 5,
    for question 2 the answer is maybe 3 ..

    so the key/value would have to look like 1:5 , 2:3 to be able to put this data in the database? Thanks!
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,542
    Rep Power
    595
    I think you want to use 'contrinue'. That will skip to the end of the current iteration, test the end condition and proceed to the next iteration if any are left.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  22. #12
  23. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    204
    Rep Power
    3
    thanks

  24. #13
  25. Confused badger
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Mar 2009
    Location
    West Yorkshire
    Posts
    1,115
    Rep Power
    487
    PHP Code:
    foreach ($antwoorden as $key => $value) {
            
    $i $key 1;
            echo 
    $i": " .$value "|";

    "For if leisure and security were enjoyed by all alike, the great mass of human beings who are normally stupefied by poverty would become literate and would learn to think for themselves; and when once they had done this, they would sooner or later realise that the privileged minority had no function and they would sweep it away"
    - George Orwell, 1984
  26. #14
  27. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    204
    Rep Power
    3
    Originally Posted by badger_fruit
    PHP Code:
    foreach ($antwoorden as $key => $value) {
            
    $i $key 1;
            echo 
    $i": " .$value "|";

    Thanks! (god that was stupid of me). much to learn! thanks

IMN logo majestic logo threadwatch logo seochat tools logo