Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

HTML Lists/Menus - Submitting multiple options with PHP

  • 13-08-2007 01:33PM
    #1
    Closed Accounts Posts: 230 ✭✭


    I've been trying to pull a list of variables between two pages with no luck. I'm just using the basic $_GET["X"]; (where X is the name of the list). When I make multiple selections, it only gives me the last one as output.

    Anyone got any ideas?

    Thanks in advance.


Comments

  • Registered Users, Registered Users 2 Posts: 68,173 ✭✭✭✭seamus


    The issue with the multiple select is the way that the browser posts or gets the variables.

    For example, if you have a select called "mySelect", and someone has selected three of the values from it, the the query string in the get, will look a little like this:

    mypage.php?mySelect=5&mySelect=7&mySelect=18

    When the variables are passed to PHP, it only sees one value for mySelect - the last one, because the previous two are overwritten.

    You need to rename the variable to include brackets at the end, so mySelect[]. This tells PHP to create an array called "mySelect", and every time a value is added to "mySelect[]", PHP adds it to the end of the array.

    So then you just access it as $_GET[0], etc.


  • Closed Accounts Posts: 230 ✭✭danindublin


    Hi Seamus,

    Thanks for your help, seems to have go me a little down the road but havent got further than the first value.

    //Select Section

    <select name="s1" size="1" multiple="multiple" class="lst2" id="s1">
    <option>Mr Ape</option>
    <option>Mr Bird</option>
    <option>Mr Cat</option>
    <option>Mr Dog</option>
    <option>Mr Elephant</option>
    <option>Mr Fish</option>
    </select>


    //Attempt A - Seems to just add one char at a time until it gets to the end of the first string, then goes blank

    $val .= $_GET[0];
    $val .= $_GET[1];
    $val .= $_GET[2];
    $val .= $_GET[3];
    $val .= $_GET[4];
    $val .= $_GET[5];

    //Attempt B - Only returns the last string

    $val .= "Start A<br/>";
    $val .= $_POST["s1"][0];
    $val .= "<br/>Get value from 0<br/>";
    $val .= $_POST["s1"][1];
    $val .= "<br/>Get value from 1<br/>";
    $val .= "Finished A<br/>";

    :confused::confused::confused:


  • Registered Users, Registered Users 2 Posts: 68,173 ✭✭✭✭seamus


    You're still not creating the array :)

    <select name = "s1[]"


  • Closed Accounts Posts: 230 ✭✭danindublin


    seamus wrote:
    You're still not creating the array :)

    <select name = "s1[]"

    :D Seamus you're a genius! Thanks a million!!!! :D


Advertisement