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">
&l开发者_如何学Ct;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...
- You should try to
error_reporting(E_ALL)
your page, so that you can see any and all errors your page comes across. - What's
connect()
? did you meanmysql_connect()
? - (Kinda unrelated) You shouldn't use
mysql_*
functions, use MySQLi (Good) or PDO (Awesome).
when i get stuck on server 500 errors i go over to http://phpcodechecker.com/ and paste in the page. when i did that on what code you have now, it complained about a missing ; on line 2.
another tip: you can use this syntax style for slightly less complex output: $out = "text {$var} {$var2} {$array['index']}".DEFINITION_ONE
surprisingly this works for html attributes which we expect to output as "value" e.g. $options .= "{$i} {$f}
- make sure your file is a .php, not .html
- don't forget "
<?php
" at the top of page. - try to write "
echo 'hello';
" at the top your page to see if it prints.
this seems trivial, but as you seem to have pasted the full code, i just wanted to make sure.
Perhaps $_SESSION['username'] isn't set? You don't have a closing brace on that if statement, so if isset($_SESSION['username']) returns false, the whole page never gets displayed.
精彩评论