what is the best, tidiest way to populate a html select tag with items from the database?
For example, if I have the following php:
$sql="SELECT a.athleteId, a.fName, a.lName FROM Athletes a, SupportStaff s, StaffAthletes sa WHERE sa.staffId = $id AND a.athleteId = sa.athleteId";
$result=mysql_query($sql);
Then:
- How do I populate the drop down menu with the list of tuples retrieved from the relation?
- How should the php, html and jQuery be integrated?
I have the following, but it doesn’t work- It just displays a blank page:
<?php
error_reporting(E_ALL)
session_start();
//connect to database
function connect() {
$dbh = mysql_connect ("localhost", "d", "a") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db("PDS", $dbh);
return $dbh;
}
if(isset($_SESSION['username'])){
$dbh = connect();
$id = $_SESSION['id'];
$sql="SELECT a.athleteId, a.fName, a.lName FROM Athletes a, SupportStaff s, StaffAthletes sa WHERE sa.staffId = $id AND a.athleteId = sa.athleteId";
$result=mysql_query($sql);
$options="";
$i = 1;
while ($row=mysql_fetch_array($result)) {
$f=$row["fName"];
$l=$row["lName"];
$options.="<OPTION VALUE=\"$i\">".$f.' '.$l."</OPTION>";
$i++;
}
?>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<script src = "jQuery.js"></script>
<script>
$(document).ready(function(){
$("#go").click(function(e){
if($("#selectathlete option:selected").val() == "0"){
alert("Please Select An Athlete");
}else{
//set hidden textfield
$("form#profile").submit();
}
});
});
</script>
<title>Personal Diary System - Home Page</title>
</head>
<body>
<h1>Home Page</h1>
<p>Select An Athlete:
<SELECT ID ="selectathlete" NAME="athletes">
<OPTION VALUE="0">Choose</OPTION>
<?php echo($options);?>
</SELECT>
</p>
<form id = "profile" name="input" action="viewathleteprofile.php" method="post">
<input type = "hidden" id = "athleteId">
<input type = "button" name = "go" id = "go" value = "Go">
</form>
</body>
</html>
I’ve been debugging for hours and it just doesn’t work…
error_reporting(E_ALL)your page, so that you can see any and all errors your page comes across.connect()? did you meanmysql_connect()?mysql_*functions, use MySQLi (Good) or PDO (Awesome).