Page 1 of 1

Dynamically generated select form help

Posted: Sat Nov 27, 2010 3:40 pm
by mlummus
I am trying to populate a select drop-down form element where it displays the "start date" but sends the "res_id" to the next script when submitted. I'm having trouble getting it right, though, and don't know what to try next. Help?

Code: Select all

$query = "SELECT res_id, start_date FROM reservations WHERE user_id=$user_id";
$error = mysqli_error($db);
echo $error;

$result = mysqli_query($db, $query);
$num_results = mysqli_num_rows($result);

echo "<p> Number of reservations found: ".$num_results."</p>";

echo "<form method='post' action='viewbill.php'>";
echo "Reservation Date: <select name = 'res_select'>";

$row=mysqli_fetch_array($result);
$res_id = $row["res_id"];
$start_date = $row["start_date"];

for ($i=0; $i < $num_results; $i++)
{
echo "<option value = '$start_date'> $start_date </option>";
}

echo "<input type='submit' value='View' />";

echo "</form>";

Re: Dynamically generated select form help

Posted: Sat Nov 27, 2010 4:14 pm
by califdon
You need to study up on PHP syntax and how to read the results of a database query:

Code: Select all

echo "<select name='startdate'>";
while ($row=mysqli_fetch_array($result))
echo "<option value = $row['$res_id']> $row['$start_date'] </option>";
}
echo "</select>";
References:
http://www.php-mysql-tutorial.com/wikis ... abase.aspx
http://htmlhelp.com/reference/html40/forms/select.html