Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Try these changes. I added comments to the code.</p> <p>Basically I added a number to the formfield names, so you can distinguis them. So the first name is <code>name_0</code>, then when you add a row with javascript the next name will be <code>name_1</code>. Then in the PHP you'll make a loop and check if there's a <code>name_0</code>, then in the next loop you'll check if there's a <code>name_1</code> and so on. When it doesn't find any more rows it exits the loop using <code>break;</code>.</p> <p>I haven't tested the code, but basically it should work. Try to implement it yourself instead of copy-paste. Read the comments and look at what I changed in the code.</p> <p>Good luck.</p> <p>HTML:</p> <pre><code>&lt;!-- Make the first fields use row number 0 --&gt; &lt;form method='post' action='newmenu.php' class='newmenu'&gt; &lt;table&gt; &lt;tr&gt;&lt;td&gt;Namn:&lt;/td&gt;&lt;td&gt;&lt;input type='text' name='title' placeholder='namn' /&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt;&lt;td&gt;Undertext:&lt;/td&gt;&lt;td&gt;&lt;input type='text' name='subtitle' placeholder='namn' /&gt;&lt;/td&gt;&lt;/tr&gt; &lt;/table&gt; &lt;ul&gt; &lt;li class='newrow'&gt; &lt;h6&gt;New item&lt;/h6&gt; &lt;div&gt; &lt;p&gt;Name:&lt;/p&gt; &lt;input type='text' name='name_0' placeholder='name' /&gt; &lt;/div&gt; &lt;div&gt; &lt;p&gt;Ingredients:&lt;/p&gt; &lt;input type='text' name='ingredients_0' placeholder='ingredients' /&gt; &lt;/div&gt; &lt;div&gt; &lt;p&gt;Price:&lt;/p&gt; &lt;input type='text' name='price_0' placeholder='price' /&gt; &lt;div&gt; &lt;/li&gt; &lt;li class="edit"&gt; &lt;input type="submit" value="Submit"&gt; &lt;p class="add"&gt;New row&lt;/p&gt; &lt;/li&gt; &lt;/ul&gt; &lt;/form&gt; </code></pre> <p>jQuery:</p> <pre><code>$(document).ready( function() { $('.newmenu').hide(); $('.zero h3 a').click(function() { $('.newmenu').slideToggle(); }); // dynamically added rows start at row 1 var row_number = 1; $('.newmenu &gt; ul li p.add').click(function(){ // append the fields with the row number $('.newmenu &gt; ul li.edit').before("&lt;li class='newrow'&gt;&lt;h6&gt;New item&lt;/h6&gt;&lt;div&gt;&lt;p&gt;Name:&lt;/p&gt;&lt;input type='text' name='name_" + row_number + "' placeholder='name' /&gt;&lt;/div&gt;&lt;div&gt;&lt;p&gt;Ingredients:&lt;/p&gt;&lt;input type='text' name='ingredients_" + row_number + "' placeholder='ingredients' /&gt;&lt;/div&gt;&lt;div&gt;&lt;p&gt;Price:&lt;/p&gt;&lt;input type='text' name='price_" + row_number + "' placeholder='price' /&gt;&lt;/li&gt;"); // increment row number row_number++; }); }); </code></pre> <p>PHP:</p> <pre><code>if($_SERVER['REQUEST_METHOD'] == 'POST'){ // start at row 0 $row_number = 0; while(true) { // check if there are still fields in this row offset if(isset($_POST['name_' + $row_number])) { $status_input = $stmt = $dbh -&gt;prepare(" INSERT INTO menues( restaurant_id, title, subtitle, name, ingredients, price, category, upload_date) VALUES (:restaurant_id,:title, :subtitle, :name, :ingredients, :price, :category, NOW()) (:restaurant_id,:title, :subtitle, :name, :ingredients, :price, :category, NOW()) (:restaurant_id,:title, :subtitle, :name, :ingredients, :price, :category, NOW()) "); $stmt-&gt;bindParam(":restaurant_id", $userdata[0]['user_id']); $stmt-&gt;bindParam(":title", $_POST['title']); $stmt-&gt;bindParam(":subtitle", $_POST['subtitle']); $stmt-&gt;bindParam(":name", $_POST['name_' . $row_number]); $stmt-&gt;bindParam(":ingredients", $_POST['ingredients_' . $row_number]); $stmt-&gt;bindParam(":price", $_POST['price_' . $row_number]); $stmt-&gt;bindParam(":category", $userdata[0]['category']); $stmt-&gt;execute(); } // increment row number $row_number++; } else { // if there are no rows anymore, exit the while loop break; } } </code></pre>
 

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