I am working on a site where I need to create forms to both add and edit
information for a group of products. I would like to use the same form for
both adding new and edit data for existing products, while at the same time
separating data and presentation functions. So when the user selects the
option to add a new product, the form comes up with all drop down lists
filled from the database with the options for that field and all other
fields blank, and when a product is selected to be edited, I would like the
same form to come up, but this time with all the fields filled in, including
all drop down lists filled with the appropriate value for that product.
In the past, I've done something like this to fill drop down lists with the
valid options:
function add_product_form()
{
?>
<div name="add_new_product" align="center">
<h3>Add New Product</h3>
<form name="add_product" method="get" action="add_product.php">
<select name="category">
<?php
if (db_connect()) //sets up conenction to database
{
$query = "select * from category";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
$id = $row['category_sku'];
$name = $row['category_name'];
?>
<option value=<?php echo $id ?>><?php echo $name ?></option>
<?php
}
}
else
{
echo("Error connecting to host");
}
}
This snippet just shows one drop down list being created and filled with the
options. So how can I create a form to use for both adding new and editing
existing products, and only fill in the data if it is editing? If there is
an OO way to do this, that would be great. One idea I have is to pass a
variable that is set to either "new" or edit", and do something based on
that, but what I'm unsure about is how to pass the edited data to the form
from the function that gets it from the database.
Thanks.
Steve