Note that there are some explanatory texts on larger screens.

plurals
  1. POStore input of dynamically created text fields
    primarykey
    data
    text
    <p>I have made a button which creates a text with name=1,2,3 ... on every click. I want to store all the inputs of these text fields in database. </p> <pre><code>&lt;?php $con = mysqli_connect("localhost", "root","", "abc"); // Check connection if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } $maxoptions = 10; // I don't want only 10 inputs from text fields // but as many as the user creates and fills for ($i = 1; $i &lt; $maxoptions; $i++) { $sql="INSERT INTO qa (q, a$i) VALUES ('$_POST[q1]', '$_POST[i]')"; // '$_POST[i]' is not working } if (!mysqli_query($con, $sql)) { die('Error: ' . mysqli_error($con)); } mysqli_close($con); ?&gt; </code></pre> <p>Now, how to create columns in the database dynamically with these text fields?</p> <p>Here is the JavaScript code with which I am creating the text fields:</p> <pre><code>var intTextBox1 = 0; //FUNCTION TO ADD TEXT BOX ELEMENT function addElement1() { intTextBox1 = intTextBox1 + 1; var contentID = document.getElementById('content1'); var newTBDiv = document.createElement('div'); newTBDiv.setAttribute('id','strText'+intTextBox1); newTBDiv.innerHTML = "Option" + intTextBox1 + ": &lt;input type='text' id='" + intTextBox1 + "' name='" + intTextBox1 + "'/&gt;"; contentID.appendChild(newTBDiv); } //FUNCTION TO REMOVE TEXT BOX ELEMENT function removeElement1() { if (intTextBox1 != 0) { var contentID = document.getElementById('content1'); contentID.removeChild(document.getElementById('strText'+intTextBox1)); intTextBox1 = intTextBox1 - 1; } } </code></pre> <p>and here is the code of button:</p> <pre><code>&lt;form id="s1form" name="s1form" method="post" action="qno1.php"&gt; &lt;input type="text" name="q1"&gt; &lt;input type="button" value="Add a choice" onClick="javascript:addElement1();" /&gt; &lt;input type="button" value="Remove a choice" onClick="javascript:removeElement1();" /&gt; &lt;div id="content1"&gt;&lt;/div&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload