/    Sign up×
Community /Pin to ProfileBookmark

Variable select box name

I am writing a little site for me and my friends to be able to pick football games and keep standings of who is better at picking and such.

I am trying to submit the picks right now but the dropdown box where the user picks the home or away team will have a variable name.

[code=php] while($row = mysql_fetch_array($result)){
echo “<tr>”;
echo “<td>”;
echo $row[‘date’];
echo”</td>”;
echo “<td>”;
echo $row[‘time’];
echo”</td>”;
echo “<td>”;
echo $row[‘away_team’];
echo”</td>”;
echo”<td>”;
echo $row[‘spread’];
echo”</td>”;
echo “<td>”;
echo $row[‘home_team’];
echo”</td>”;
echo”<td>”;
echo”<select name='”.$row[‘gameid’].”‘>
<option value=’home’>Home</option>
<option value=’away’>Away</option>
</select>”;
echo”</td>”;
echo”</tr>”;
$i++;
}[/code]

Does anyone have any ideas how I will work this into getting that data?

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@BIOSTALLOct 24.2010 — Add a hidden field like so:

[code=php]while($row = mysql_fetch_array($result)){
echo "<tr>";
echo "<td>";
echo $row['date'];
echo"</td>";
echo "<td>";
echo $row['time'];
echo"</td>";
echo "<td>";
echo $row['away_team'];
echo"</td>";
echo"<td>";
echo $row['spread'];
echo"</td>";
echo "<td>";
echo $row['home_team'];
echo"</td>";
echo"<td>";
echo"<select name='".$row['gameid']."'>
<option value='home'>Home</option>
<option value='away'>Away</option>
</select>";
echo "<input type='hidden' name='game_id[]' value='".$row['gameid']."' />";
echo"</td>";
echo"</tr>";
$i++;
} [/code]


Then loop through the game_ids (from the hidden field we've added) like so:

[code=php]foreach ($_POST['game_id'] as $game_id) {
echo 'My game_id is '.$game_id.'<br />';
}[/code]


Hope that helps ?
Copy linkTweet thisAlerts:
@NogDogOct 24.2010 — Use square bracket notation to name the select elements, then you'll have a sub-array within the $_POST array that you can process, such as with a foreach() loop:

[code=php]
echo"<select name='game[".$row['gameid']."]'> . . . ";
[/code]

Then in the form handler you can process each selection with something like:
[code=php]
foreach($_POST['game'] as $game_id => $value) {
// do interesting stuff . . .
}
[/code]
Copy linkTweet thisAlerts:
@Xtrme_XJauthorOct 25.2010 — Thanks!
×

Success!

Help @Xtrme_XJ spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...