Drop Down Lists

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
BelowZero
Forum Newbie
Posts: 9
Joined: Tue Feb 15, 2011 12:08 pm

Drop Down Lists

Post by BelowZero »

I found a script which creates a nice drop down list from a mysql database (a list of football teams). What I want to do is select a team by name, then insert the team_id into a "reference" field in another table.
Once a team is selected, how do I return just the team_id?

----drop down list code----

Code: Select all

<?php
include("opendatabase.php");

$querya="SELECT team_id,team_name FROM teams";

$resulta = mysql_query ($querya);

echo "<select team_name=option value=''></option>";

while($hometeam=mysql_fetch_array($resulta))
{
echo "<option value=$hometeam[team_id]>$hometeam[team_name]</option>";
}

echo "</select>";
?>
----end code----

I want to create a variable ($hometeam) that is just the team_id number so I can pass it to my insert.php file.
Thanks for helping a newbie!!
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Drop Down Lists

Post by AbraCadaver »

You need a form around that with a submit button that submits to insert.php. Also the select should look more like this:

Code: Select all

echo "<select name=\"team_name\"></option>";
Then when you press the submit button the id will be available in $_POST['team_name'].
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
BelowZero
Forum Newbie
Posts: 9
Joined: Tue Feb 15, 2011 12:08 pm

Re: Drop Down Lists

Post by BelowZero »

Thanks for the reply. Got it working as expected.

Now for part 2:
Can I use this drop down list twice on the same page and assign different values to the 2 choices? I'm trying to choose a home team and an away team to put in the database, both from the same page.

http://www.beat-the-spread.net/admin_editschedule.php

I guess I need a way to pick 2 team_id's and assign them different names so I can post them both to the database at the same time.

Thanks for helping me out.
Post Reply