Dynamically generated select form help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mlummus
Forum Newbie
Posts: 5
Joined: Tue Apr 20, 2010 8:22 pm

Dynamically generated select form help

Post 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>";
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Dynamically generated select form help

Post 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
Post Reply