I have a functional HTML form and I am inserting it's contents to SQL database via PHP. I need to find a way to be able to insert multiple rows on just one click (customers will dynamically add more form fields as they wish).
This is the HTML field (part of the form):
<label for="code">Súťažný kód</label>
<input type="text" name="code" id="code">
I am then passing the field value to external .php file using jQuery:
jQuery.ajax({
url:"insert.php",
method:"POST",
data:form_data,
success:function(data) {
console.log(data);
}
});
In the insert.php I am connecting to my MySQL database, and I am getting the form value like this:
$code = $_REQUEST['code'];
Than, I am able to run necessary SQL queries to insert the row, with the all necessary values (including the 'code').
This form will be used as a submit form for a contest. Next to the 'code' form field there will be a PLUS icon (or something like that), and by clicking that icon, another field for the code will appear. The user will be able to add up to 20 codes, to 20 different fields.
My question is, how can I pass all the forms (or all the codes) to the .php file, and insert them into my MySQL database as separate rows?
Example:
User sends a form with 3 codes inserted. The resulting form would look like this:
[firstName]
[lastName]
[code]
[code]
[code]
After sending the form, these three rows should be added to the database:
# | firstName | lastName | code
----------------------------------
1 | John | Doe | 12345
2 | John | Doe | ABCDE
3 | John | Doe | 55555