Gabriel Alain
Gabriel Alain

Reputation: 1

inserting values of dynamically added form elements into mysql database

I'm making a supplies requisition form where the user can request supplies. I did this tutorial that allowed me to dynamically add form elements. I was able to produce duplicates of Stock_No, Quantity_Available and Quantity text fields and an Item_Name dropdown list. How am I suppose to insert the values that the user inputs in the dynamically added form elements?

Upvotes: 0

Views: 385

Answers (1)

John Conde
John Conde

Reputation: 219794

Each dynamically added form element should be using array syntax for the name of each field. Then when the form is submitted your PHP script should be receiving array values for each item. At that point you just need to loop through the arrays to add them to the database.

Basic Example:

$item_names  = $_POST['item_names'];
$item_prices = $_POST['item_prices'];
$item_qty    = $_POST['item_qty'];

$num_items = count($item_names);
for ($i = 0; $i < $num_items; $i++)
{
    // Create your SQL
}

Upvotes: 1

Related Questions