Page 1 of 1
can't get form with drop down box working.. $_GET/$_POST ?!
Posted: Tue Apr 10, 2007 6:01 am
by thunderstorm
Hi, im trying to use a drop down box to select data from my tables in my database that relate to just one column, which populates the drop down. I'm trying to display a drop down box full of race names pulled from a race table in my database, and this works fine, they're all listed correctly.
I want to then allow the user to select one, hit submit or whatever, then below, (OR on another page, I don't mind) in a table, display the member first and last names (from member table), the race date, length (from race table) and the result (from result table).
At the moment I'm stuck as whatever u select nothing happens. I'm really confused about how to get the race name from the submitted drop down, do you use get or post or what?? Need a simple explanation of what ive done wrong, I KNOW its wrong but just cant figure out how to fix it, getting muddled

!
Thanks, and here's my code.. at the moment i have tried to post to the same page, but dont know how to check it, i know the line if (!isset($_GET['race'])) is incorrect but dont know what to put in its place (trying to see if user has selected a race name from the drop down).. thanks
Code: Select all
<?php
require_once('connection.php');
if (!isset($_GET['race'])) {
$res=mysql_query("select race_name from race");
if(mysql_num_rows($res)==0) {
echo "there is no data in table..";
}
else {
echo'<form action="bugger.php" method="post"><select name="categories">';
for($i=0;$i<mysql_num_rows($res);$i++) {
$row=mysql_fetch_assoc($res);
echo"<option>$row[race_name]</option>";
} // endof for
echo'</select><input type="submit" name="submit" value="race" />
<input type="hidden" name="submitted" value="TRUE" /></form>';
}
}
elseif ((isset($_GET['race']))) {
$getrace = $_GET['race'];
$res=mysql_query("select member.first_name,member.last_name,race.race_name,race.race_date,results.time from member,race,result WHERE race.race_name='".$getrace."'");
}
if(mysql_num_rows($res)==0) {
echo "there is no data in table..";
}
else {
//table header
echo'<table align="center" cellspacing="10" cellpadding="5">
<tr><td align="left"><b>First Name</b></td>
<td align="left"><b>Last Name</b></td>
<td align="left"><b>Race Name</b></td>
<td align="left"><b>Race Date</b></td>
<td align="left"><b>Race Time</b></td></tr>';
echo'</select>';
}
// fetch and print all the records
while($row = mysql_fetch_array($res,MYSQL_ASSOC)) {
echo'<tr><td align="left">'.$row['first_name'].'</td>
<td align="left">'.$row['last_name'].'</td>
<td align="left">'.$row['race_name'].'</td>
<td align="left">'.$row['race_date'].'</td>
<td align="left">'.$row['time'].'</td>
</tr>
';}echo'</table>';
?>
called the page bugger.php btw, dont ask.
thanks
Posted: Tue Apr 10, 2007 8:15 am
by andym01480
Your form action is to POST the data so the data will be in the $_POST array rather than the $_GET array. As an aside $_GET is "got" from the url eg pagename.php?variable=value -
. POST is posted in the headers rather than the url.
Also you don't have a form field called "race" so that will be empty!
Code: Select all
echo'<form action="bugger.php" method="post"><select name="categories">';
Try looking in
or changing the name of the select to race!
Posted: Tue Apr 10, 2007 11:25 am
by RobertGonzalez
Something along the lines of that the user wanted in
this post?
Posted: Tue Apr 10, 2007 4:10 pm
by thunderstorm
thanks for your input, i got it working but got some more questions.
1) is there any way i can have the drop down NOT disappear when u select a race from it? atm when u pick one and hit submit it disappears.
2) I didn't realise that I have some race names that are the same, but their race lengths are different - so my drop down box looks like it has a duplicate race name as users aren't going to know which one is which.... basically, is there some way that i could somehow print the race length next to the race name in the drop down? E.g. Forest 8 (race name) - 8 miles (race length) or for the other one its Forest 8 - 5 miles... So is it possible to effectively concatenate it? Or will this <span style='color:blue' title='I'm naughty, are you naughty?'>smurf</span> up the query later??
here's my current code.
Code: Select all
<?php
require_once('connection.php');
if (!isset($_POST['submit']))#IT should be $_POST bcoz form method is POST.
{
$res=mysql_query("select race_name from race");
if(mysql_num_rows($res) == 0) {
echo "there is no data in table..";
}
else
{
echo '<form action="race_results.php" method="post"><select name="race">';
while($row = mysql_fetch_array($res))
{
print '<option value = "'.$row['race_name'].'" >'.$row['race_name'].'</option>';
} // endof for
echo'</select><input type="submit" name="submit" value="submit" />
<input type="hidden" name="submitted" value="TRUE" /></form>';
}
}
else{
$getrace = $_POST['race'];
$res = mysql_query("select member.first_name,member.last_name,race.race_name,race.race_date,results.time from member,race,results WHERE member.member_no = results.member_no AND race.race_no = results.race_no AND race.race_name='".$getrace."'");
if(mysql_num_rows($res) == 0) {
echo "there is no data in table..";
}
else {
//table header
echo'<table align="center" cellspacing="10" cellpadding="5">
<tr><td align="left">First Name</td>
<td align="left">Last Name</td>
<td align="left">Race Name</td>
<td align="left">Race Date</td>
<td align="left">Race Time</td></tr>';
// fetch and print all the records
while($row = mysql_fetch_array($res)) {
echo'<tr><td align="left">'.$row['first_name'].'</td>
<td align="left">'.$row['last_name'].'</td>
<td align="left">'.$row['race_name'].'</td>
<td align="left">'.$row['race_date'].'</td>
<td align="left">'.$row['time'].'</td>
</tr>';
}
echo'</table>';
}
}
?>
thanks

Posted: Tue Apr 10, 2007 4:52 pm
by andym01480
2) I didn't realise that I have some race names that are the same, but their race lengths are different - so my drop down box looks like it has a duplicate race name as users aren't going to know which one is which.... basically, is there some way that i could somehow print the race length next to the race name in the drop down? E.g. Forest 8 (race name) - 8 miles (race length) or for the other one its Forest 8 - 5 miles... So is it possible to effectively concatenate it? Or will this smurf up the query later??
If that is your problem do you have a column that allows for unique identification of each race record? something like race_id that is an integer. If so select teh race name and race_id
and change your select top this...
Code: Select all
echo '<form action="race_results.php" method="post"><select name="race">';
while($row = mysql_fetch_assoc($res))
{
print '<option value = "'.$row['race_id'].'" >'.$row['race_name'].'</option>';
} // endof for
echo'</select><input type="submit" name="submit" value="submit" />
<input type="hidden" name="submitted" value="TRUE" /></form>';
}
}
the option value has the unique id while the $row['race_name'] is the English version! You could also use concatenation
Code: Select all
$row['race_name'] . $row['other_race_details']
The reason being is the syntax of select is this
Code: Select all
<select name=variable>
<option value="value that will get posted">Whatever guff you want that doesn't get sent in the form but effectively ids the value to the user</option>.....
</select>
If you don't have a unique id column - get one! It needs to auto-increment and be set a primary key. Use phpmyadmin to add one! It will auto generate the numbers for your current records. Then change your code to use race_id as the identifier of a race.
Posted: Tue Apr 10, 2007 4:59 pm
by thunderstorm
wow, thanks for that, very useful informative post
yes, i have a 'id' field thats the primary key and auto increments, called race_no.
so if i changed to that:
Code: Select all
echo '<form action="race_results.php" method="post"><select name="race">';
while($row = mysql_fetch_assoc($res))
{
print '<option value = "'.$row['race_no'].'" >'.$row['race_name'].'</option>';
} // endof for
echo'</select><input type="submit" name="submit" value="submit" />
<input type="hidden" name="submitted" value="TRUE" /></form>';
}
}
and then add $row['race_name'] . $row[race_distance] to the bit in between the option tags it should solve this? and of course add race_distance and race_no to the select statement? just checking i understood your post correctly, thanks

Posted: Tue Apr 10, 2007 4:59 pm
by RobertGonzalez
To get the form to show even after a selection, move it outside of the if/else construct so no matter what it will always display.
Posted: Tue Apr 10, 2007 5:09 pm
by thunderstorm
cheers, only one question left (i hope

), how can i put a space or a - or something between the race name and race distance?
print '<option value = "'.$row['race_no'].'" >'.$row['race_name'].$row['race_distance'].'</option>';
is there any space type 'thing' that'll work in the middle?
Posted: Tue Apr 10, 2007 5:09 pm
by andym01480
Kind of understood!
and then add $row['race_name'] . $row[race_distance] to the bit in between the option tags it should solve this? and of course add race_distance and race_no to the select statement? just checking i understood your post correctly
Yes to between the option tags - you can put whatever you like there! That's what the user sees inthe drop down - what gets sent is what the value inside the option tag.
No to the select - select name should really be "race_no" for clarity of coding as you are using the drop down (if I have understood you) to select one unique race - and in your db, race_no's job is to uniquely identify a race. The
can then be used as part of a mysql_select query to get all the data for that race.
Posted: Tue Apr 10, 2007 5:11 pm
by thunderstorm
hiya, i changed it to race_no yes

and now use race_no in the later select statement to populate my table. thanks, didnt know you could do it this way,much better, learnt lots
just the space thing now

as its plonking them right next to each other and looks a little untidy.
Posted: Tue Apr 10, 2007 5:11 pm
by RobertGonzalez
It is only because you are actually trying stuff
Code: Select all
<?php
print '<option value = "' . $row['race_no'] . '" >' . $row['race_name'] . ' - ' . $row['race_distance'] . '</option>';
?>
Posted: Tue Apr 10, 2007 5:14 pm
by thunderstorm
Everah wrote:It is only because you are actually trying stuff
Code: Select all
<?php
print '<option value = "' . $row['race_no'] . '" >' . $row['race_name'] . ' - ' . $row['race_distance'] . '</option>';
?>
YES! hehe. it worked

i was trying to do it pretty similar and turned out i forgot the second . after it

brilliant, you have no idea how happy i am (sad that isn't it

)

Posted: Tue Apr 10, 2007 5:19 pm
by RobertGonzalez
Glad we could help.