Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Sep 2006
    Posts
    92
    Thanks
    4
    Thanked 1 Time in 1 Post

    Processing forms

    Hello people,

    I got a little issue here that I can't figure out yet, I have a form with 2 selections of which one can have multiple options selected:

    PHP Code:
    <form action="vote.php" method="get">
    <table align="center">
        <tr>
            <td width="150"><b>Select Commander:</b></td>
            <td width="150"><b>Select Council:</b></td>
        </tr>
        <tr>    
            <td width="150"><select name="VoteCL" size="6">
            <?php 
                $list 
    pilotVoteList $pilot["ColonyID"] );
                foreach( 
    $list as $name ) {
                    echo (
    "<option value=\"{$name["PilotID"]}\">{$name["PilotName"]}</option>");
                }
            
    ?>
            </select></td>
            <td width="150"><select name="VoteCouncil" multiple size="6" onchange="countSelected(this,4)">
            <?php 
                $list 
    pilotVoteList $pilot["ColonyID"] );
                foreach( 
    $list as $name ) {
                    echo (
    "<option value=\"{$name["PilotID"]}\">{$name["PilotName"]}</option>");
                }
            
    ?>
            </select></td>
        </tr>
        <tr>
            <td align="center" colspan="2"><input type="submit" value="Cast Vote" class="blue"></td>
          </tr>
    </table>
    </form>
    Now, this works great... the code send over (I used GET method so I could see the URL generated) is giving me something like vote.php?VoteCL=12&VoteCouncil=85&VoteCouncil=37&VoteCouncil=142

    In the vote.php my problem lies, it's the first time I use multiple on the selection and so my standard method of $b = $_GET['VoteCouncil'] doesn't work anymore as it just gives me the last value (in the above example 142).

    I have tried using $b = explode("&", $_GET['VoteCouncil']); but this also didnt have the effect I was hoping for.

    Any suggestions as to how to get this to work?

    Thanks,
    T.
    Last edited by twomt; 06-03-2007 at 07:01 PM.

  • #2
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,046
    Thanks
    19
    Thanked 42 Times in 42 Posts
    $_GET['VoteCouncil'] is an array so add [] on the end
    for each them if you need
    PHP Code:
    <?php
    //$_GET['VoteCouncil'][0] == first value of the querystring
    foreach ( $_GET['VoteCouncil'] as $key) {
    echo 
    $key;
    }
    ?>
    also rap up your codes in the appropriate tags [_php_] [_/php_] with no underscores, helps us read it

  • #3
    New Coder
    Join Date
    Sep 2006
    Posts
    92
    Thanks
    4
    Thanked 1 Time in 1 Post
    Hi,

    If I do this:

    PHP Code:
    foreach ( $_GET['VoteCouncil'] as $key ) {
    echo ( 
    $key );

    then I get :

    Warning: Invalid argument supplied for foreach()

    (and I used the tags... didn't know these were there...)

  • #4
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,046
    Thanks
    19
    Thanked 42 Times in 42 Posts
    what happens if you add [] onto the end of it like $_GET['VoteCouncil'][] or print_r($_GET)

  • #5
    New Coder
    Join Date
    Sep 2006
    Posts
    92
    Thanks
    4
    Thanked 1 Time in 1 Post
    PHP Code:
    foreach ( $_GET['VoteCouncil'][] as $key ) {
        echo ( 
    $key );

    gives:

    Fatal error: Invalid opcode 81/4/8.

    The print_r shows the following:

    Array ( [VoteCouncil] => 142 ) , which again is just the final option I selected... the URL is vote.php?VoteCouncil=20&VoteCouncil=37&VoteCouncil=12&VoteCouncil=142

  • #6
    Regular Coder
    Join Date
    Apr 2007
    Posts
    295
    Thanks
    0
    Thanked 19 Times in 19 Posts
    Change your selects to
    Code:
    <select name="VoteCL[]" size="6">
    
    <select name="VoteCouncil[]" multiple size="6" onchange="countSelected(this,4)">
    Then $_GET['VoteCL'] and $_GET['VoteCouncil'] should both be arrays that you can send through a foreach

  • #7
    New Coder
    Join Date
    Sep 2006
    Posts
    92
    Thanks
    4
    Thanked 1 Time in 1 Post
    Quote Originally Posted by mr e View Post
    Change your selects to
    Code:
    <select name="VoteCL[]" size="6">
    
    <select name="VoteCouncil[]" multiple size="6" onchange="countSelected(this,4)">
    Then $_GET['VoteCL'] and $_GET['VoteCouncil'] should both be arrays that you can send through a foreach
    Cheers for that, got it working now.


  •  

    Posting Permissions

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