...

View Full Version : Processing forms



twomt
06-03-2007, 05:41 PM
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:


<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.

rafiki
06-03-2007, 05:59 PM
$_GET['VoteCouncil'] is an array so add [] on the end
for each them if you need


<?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 :)

twomt
06-03-2007, 06:07 PM
Hi,

If I do this:


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...)

rafiki
06-03-2007, 06:13 PM
what happens if you add [] onto the end of it like $_GET['VoteCouncil'][] or print_r($_GET)

twomt
06-03-2007, 06:18 PM
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

mr e
06-03-2007, 07:15 PM
Change your selects to


<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

twomt
06-03-2007, 10:44 PM
Change your selects to


<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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum