Aha! Seeing the comment box makes it clear. So it is a two-step process - they choose the project, then choose the task and enter a comment? This means you will need to get the form to submit when they choose the project, but not save anything to the database until they click Add.
This requires 3 main things:
1) when the project selection changes, run some javascript to submit the form, and set some field to say not to save to the db.
2) make sure the previously selected/entered values are maintained in the form between postbacks.
3) save details when they press Add
Here is one way you could do this:
Code:
<?php
$db = mysql_connect("localhost", "root", "password");
mysql_select_db("testing");
// set to 1 if we changed project
if(isset($_POST['projchanged']) && $_POST['projchanged'] != '1')
{
// assume they clicked add so can save to db
$projectid = $_POST['pid'];
$taskid = $_POST['tid'];
$comment = $_POST['comment'];
// add to db
mysql_query("insert into tasknotes VALUES ($projectid, $taskid, '$comment')");
}
?>
<html>
<head></head>
<body>
<form name="addTime" method="post" action="insertTime.php">
<h4>Add Time:</h4><br />
<table>
<tr>
<td>Project:</td>
<td>
<select name="pid" onchange="document.getElementById('projchanged').value = '1'; document.forms['addTime'].submit();">
<!-- default "no project selected" option -->
<option value=""></option>
<?php
$resultProject = mysql_query("SELECT pid, project_name FROM Projects;");
while ($row=mysql_fetch_array($resultProject)) {
$pid = $row["pid"];
$value = $row["project_name"];
// keep same entry selected
$isSelected = ($_POST['pid'] == $pid);
?>
<option value="<?php print $pid; ?>" <?php if($isSelected) print "selected"; ?>><?php print $value; ?></option>
<?php } ?>
</select>
</td>
</tr>
<?php if(isset($_POST['pid']) && $_POST['pid'] != "") {
// check they have chosen a project - initially hidden
?>
<tr>
<td>Task:</td>
<td>
<select name="tid">
<?php
// get the project id they selected
$projectID = $_POST['pid'];
$sqlTask = "SELECT * from tasks where pid = $projectID;";
$resultTask=mysql_query($sqlTask);
while ($row=mysql_fetch_array($resultTask)) {
$tid=$row["tid"];
$value=$row["task_name"];
// keep same entry selected if necessary
$isSelected = ($_POST['tid'] == $tid);
?>
<option value="<?php print $tid; ?>" <?php if($isSelected) print "selected"; ?>><?php print $value; ?></option>
<?php } ?>
</select>
</td>
</tr>
<?php } ?>
<tr>
<td>Comments:</td>
<td><textarea name="comment" rows="10" cols="20"><?php print $_POST['comment']; ?></textarea></td>
</tr>
</table>
<input type="submit" value="Add" />
<input type="reset" value="Reset" />
<!-- use this so we know if they changed project or not -->
<input type="hidden" name="projchanged" id="projchanged" value="0" />
</form>
</body>
</html>
If you are passing to a different page, this could be a bit trickier - maybe use javascript to change the action of the form when click add.
Hope this makes sense
Phil