0

I'm working on a project where the user will get an online form they have to fill out with items. I have many different labels for personal data and one label for the item name, that they will have to select from a dropdown (items come from the database).

<label id="first">Item name</label><br/>
<select name="itemname" required>
    <?php 
    $sql = mysqli_query($con, "SELECT name FROM items");
    while ($row = $sql->fetch_assoc()){
        $naziv = $row['name'];
    echo '<option value="'.$naziv .'">'.$naziv .'</option>';
    }
    ?>
</select><br/>

Now this code is working perfectly, and I also have a quantity field for it. Now, I don't have a clue how I could add the option to add multiple input fields without having to declare many more database columns and separate inputs for each one. Could I somehow do this dinamically?

4
  • Are you talking about creating a list of <input> tags, each with a <label>? If so, how about creating a database table called "inputs"? Commented Apr 10, 2020 at 15:51
  • Each form has a field named Item name and Item quantity. Item name has a dropdown of all items. I'm trying to add like 15 fields for both of them without having to have 15 columns for it in my database. Database: id, itemname, itemquantity, date, personaldata... Commented Apr 10, 2020 at 16:45
  • You have multiple forms? What is the source for these 15 fields? Still confused. Can you post an example of the completed HTML? Commented Apr 10, 2020 at 21:01
  • I currently don't have my files with me but imagine, that a user has a reciept where they enter personal data. On the reciept they enter a maximum of 15 items but a minimum of 1. The source for them is a database with all items, and the fields should post their content to the database. I'm not sure how to form a database for allowing multiple items on one reciept that users can input. Commented Apr 11, 2020 at 8:24

1 Answer 1

0

You want a database structure that permits receipts that can contain multiple items.

You need multiple tables.

Create a new table called 'receipts':

TABLE receipts: id, (...personal data)

Add a field to the items table called "receipt_id".

TABLE items: id, receipt_id, itemname, itemquantity, date

You should also have a separate table to contain the available item names, which you use to populate the <select>, above:

TABLE itemnames: id, name, (description?)
Sign up to request clarification or add additional context in comments.

2 Comments

I've been thinking in this direction but how could I implement this on the html form? For example, the user can click a + sign to add more items?
You'll need to write a JavaScript function that adds new inputs to the form.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.