0

I have a form that a user can add multiple textareas to build the form before submitting to the mysql database.

I need to add a new row to the questions_table for every question. This is key: 1 form fires multiple insert queries. One for every question textarea.

Here is a minimal example of one table row:

Here is a minimal example of the table:

There are other hidden fields in the in the form not listed below like id, user, ordering, ect... But I can only have one question column per row.

I would like to use the form submit button to loop thru each question textarea and insert one question per row. What is the best way to go about doing this?

    <form action="index.php?event=submit" id="form1">
<textarea rows="4" cols="30" name="question" form="form1">Enter text</textarea>
<textarea rows="4" cols="30" name="question" form="form1">Enter text</textarea>
<textarea rows="4" cols="30" name="question" form="form1">Enter text</textarea>
<textarea rows="4" cols="30" name="question" form="form1">Enter text</textarea>
      <input type="submit">
    </form>
Craig Martin
  • 161
  • 1
  • 1
  • 12
  • 2
    Name your textareas like `question[]` then you can read the params in the server side using PHP like `$_POST['question']` it will be an array then you can iterate over and insert to db – Arun P Johny Mar 19 '15 at 00:52
  • 1
    `$_POST['question']` and not `$_POST('question')` @ArunPJohny – Funk Forty Niner Mar 19 '15 at 00:54
  • 1
    @Fred-ii- yes you are right – Arun P Johny Mar 19 '15 at 00:56
  • possible duplicate of [Multiple inputs with same name through POST in php](http://stackoverflow.com/questions/7880619/multiple-inputs-with-same-name-through-post-in-php) – Simon MᶜKenzie Mar 19 '15 at 01:27
  • I do not want to change the name. Each question is its own row NOT column. So, this is NOT a duplicate post. – Craig Martin Mar 19 '15 at 01:48
  • Again, the loop should insert a new row for each textarea in the form. – Craig Martin Mar 19 '15 at 01:54
  • _“I do not want to change the name”_ – well then you can’t get it to work with PHP alone, because PHP will _overwrite_ the values of multiple parameters of the same name, unless the square bracket naming scheme is used. Of course you can go get the content of the input fields in a JavaScript loop, and then make an AJAX request for each single one to transfer the content to your server-side script … – CBroe Mar 19 '15 at 01:57
  • That sounds good. Do you have an example of this anywhere. – Craig Martin Mar 19 '15 at 02:01

0 Answers0