Hello!
I have a form on my page where you can enter different fields and then process them to be written on a database. I also have a select list, that retrieves data from my database. I now want one of the fields to be "filled" with whatever I select from the list. How do I do that???
see the code below:
cheers
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Enter new Task</title>
</head>
<form method="POST" action="input.php">
<body>
<center>
<h1>New Task</h1>
<h3>Please fill in the details below to enter a new task. Fields shown in <font color="red">red</font> are mandatory.</h3>
<?php
include 'db.php';
include 'error.php';
function selectDistinct ($connection,
$tableName,
$columnName,
$pulldownName,
$additionalOption,
$defaultValue)
{
$defaultWithinResultSet = FALSE;
// Query to find distinct values of $columnName
// in $tableName
$distinctQuery = "SELECT DISTINCT $columnName
FROM $tableName";
// Run the distinctQuery on the databaseName
if (!($resultId = @ mysql_query ($distinctQuery,
$connection)))
showerror();
// Retrieve all distinct values
$i = 0;
while ($row = @ mysql_fetch_array($resultId))
$resultBuffer[$i++] = $row[$columnName];
// Start the select widget
echo "\n<select name=\"$pulldownName\">";
// Is there an additional option?
if (isset($additionalOption))
// Yes, but is it the default option?
if ($defaultValue == $additionalOption)
// Show the additional option as selected
echo "\n\t<option selected>$additionalOption";
else
// Just show the additional option
echo "\n\t<option>$additionalOption";
// check for a default value
if (isset($defaultValue))
{
// Yes, there's a default value specified
// Check if the defaultValue is in the
// database values
foreach ($resultBuffer as $result)
if ($result == $defaultValue)
// Yes, show as selected
echo "\n\t<option selected>$result";
else
// No, just show as an option
echo "\n\t<option>$result";
} // end if defaultValue
else
{
// No defaultValue
// Show database values as options
foreach ($resultBuffer as $result)
echo "\n\t<option>$result";
}
echo "\n</select>";
} // end of function
// Connect to the DBMS
if (!($connection = @ mysql_connect($hostName,
$username,
$password)))
die("Could not connect to database");
if (!mysql_select_db($databaseName))
showerror();
echo "\nPriority: ";
// Produce the select list
// Parameters:
// 1: Database connection
// 2. Table that contains values
// 3. Attribute that contains values
// 4. <SELECT> element name
// 5. An additional non-database value
// 6. Optional <OPTION SELECTED>
selectDistinct($connection,
"priority",
"status",
"status",
"All",
"All");
?>
<table>
<col span="1" align="right">
<tr>
<td><font color="red">Name:</font></td>
<td><input type="text" name="employee_name" size=50></td>
</tr>
<tr>
<td><font color="red">Company:</font></td>
<td><input type="text" name="company" size=50></td>
</tr>
<tr>
<td><font color="red">Priority</font></td>
<td><input type="text" name="priority" size=50 value="!!!this is where I want the selected item to be,so I can process it to my database!!!!"</td>
</tr>
<tr>
<td><font color="red">Task</font></td>
<td><input type="text" name="task" size=50></td>
</tr>
<tr>
<td><input type="submit" value="Submit"></td>
</tr>
</table>
</form>
</center>
</body>
</html>