Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP Select Box and default Value 1

Status
Not open for further replies.

pastorandy

IS-IT--Management
Nov 2, 2006
84
0
0
GB
I have this which pulls a country select box:
Code:
<?
$query_country  = "SELECT * FROM country";
$result_country =  mysql_query($query_country) or die (mysql_error());

 echo "<select name = country>"; 
     
     while ($row2 = mysql_fetch_array($result_country)) 
	 echo "<option value = '$row2[country_name]'>$row2[country_name]</option>";
        }
	 
	 } echo "</select>";
?>

How would I set a default country?
 
Code:
<?
[red]$defaultCountry = 'GB';[/red]
$query_country  = "SELECT * FROM country";
$result_country =  mysql_query($query_country) or die (mysql_error());

echo "<select name=[red]\"country\"[/red]>"; 

while ($row2 = mysql_fetch_array($result_country)) [red]:[/red]
   [red]$country = htmlspecialchars($country);
   $selected = $defaultCountry ==$country  ? 'selected="selected"' : '';[/red]
   echo "<option value=[red]\"$country" $selected[/red] >$country</option>";
endwhile;
 
echo "</select>";
?>
 
Thanks jpadie. Just worked out who you are :eek:)
How bizarre is that?

Andy
 
JP
Not working for me. No list displayed. Is it something to do with the country being country_name?
 
good to see you back at TT, Andy.

well, you need to specify defaultCountry to something that is actually in the list. note that it needs to be the right caps etc. i assume that's not the problem though ...

and also this line is wrong (correct as shown)

Code:
$country = htmlspecialchars([red]$row2['country_name'][/red]);
 
Just been tweaking it and go this to work:
Code:
<?
$default_country = 'United Kingdom';
$query_country  = "SELECT * FROM country";
$result_country =  mysql_query($query_country) or die (mysql_error());

 echo "<select name = country>"; 
     
     while ($row2 = mysql_fetch_array($result_country)) 
	 {
 $selected = $default_country ==$row2['country_name']  ? 'selected="selected"' : '';
  echo "<option value = '$row2[country_name]' $selected>$row2[country_name]</option>";
           
     } echo "</select>";
?>
 
i have the same problem

Code:
echo "<table border = '5'>";
echo "<tr>";
echo "<form name='Set' method='post' action='Set_Team.php'>";
echo "<td>";
echo "<table border = '0'>";
while($row = mysql_fetch_array($data))
{
    $ID = $row['ID'];
    echo "<tr>";
    echo "<td>";
    echo $row['Player'];
    echo "</td>";
    echo "<td>";
    echo "<select name='$ID'>";
    echo "<option name='Start'>Start</option>";
    echo "<option name='Sit'>Sit</option>";
    echo "</select>";
    echo "</td>";
    echo "</tr>";
}

echo "</table>";
echo "<center>";
echo "<input type='hidden' name='TN' value=$TeamName />";
echo "<input type='submit' name='Set' value='Set Players' />";
echo "</td>";
echo "</tr>";
echo "</form>";
echo "</table>";

$setquery="SELECT * from $TeamName";
$setdata=mysql_query($setquery);

while($setrow = mysql_fetch_array($setdata))
{
    $Set=$setrow['ID'];
    //echo $Set;
    //echo $_POST[$Set];
    $setquery2 = "UPDATE $TeamName set Start_Sit=$_POST[$Set] where ID=$Set";
    mysql_query($setquery2);
    mysql_error();
}

but when i try to set the option value to what the value is in the table it does not stay it always says "Start" even if i select "Sit"

what am i doing wrong?
 
you are setting the name of a select box to an id. that is unlikely to be a good idea.
you are not setting the select attribute nor testing the value of the relevant database field against the value of the relevant select option.
 
I see now I would need to have the value as selected when retrieving the value from the database
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top