Hi,
I'm trying to make a recipe page using PHP and MySQL. For this I decided to make two tables (recipe and ingredients) and have a one-to-many relationship (because 1 recipe can have many ingredients). Structure of the table is as follows:
Table Recipe {
id int not null auto_increment,
name varchar(30),
directions text,
date date,
submitted_by varchar(30),
picURL varchar(50)
}
Table Ingredients {
ing_id int not null auto_increment,
id int,
quantity int,
units varchar(15),
items varchar(50)
}
Ok here's what I'm trying to do. In a single page, I'd like to be able to add/edit/delete all the above rows/fields. However, I'd like the ingredient form to be dynamic because there could be just one ingredient, or there could be ten, or hundred. However, it is unpleasant to show 100 rows for the ingredient form when all we might need (usually) might be 5 or 6. For now my solution was a loop and counter, with PHP's ability to print out html codes:
if (isset ($_POST['i_value'])) { $i= $_POST['i_value']; }
As you can see I've used a hidden form to store $i's value and within the document I have a button which increments the hidden value via javascript. The textfields of ingredients have been made into arrays too in the form (name = "textfield[$i]").
But when I do want to add new rows for the ingredients, anything I've typed up in the other fields disappear and will have to start over again. In other words, it has to reload because for $i to attain its value, $i_value has to be submitted.
Is there any other way I can still make it dynamic, while not reloading the page? I've thought about using session variables, but it seems to be a big variable for something as little as this.
😕