Note that there are some explanatory texts on larger screens.

plurals
  1. POscript runs many times when it should only run once
    text
    copied!<p>I have a nagging problem that I have spent many hours trying to resolve with no luck. I have a form that is used to remove items from our database. Here is the form: <strong>EDIT:</strong> I have updated the code to something that is a little more reasonable, however I still have my problem.</p> <pre><code> &lt;div id="tabs-3"&gt; &lt;p&gt;Select Replenish for Books that will be for sale again.&lt;br/&gt;Select Remove for Books that will NOT be for sale again.&lt;/p&gt; &lt;br/&gt; &lt;table&gt; &lt;tr&gt; &lt;td&gt;&lt;input type="radio" name="returnType" value="replenish" checked&gt;Replenish&lt;br&gt;&lt;/td&gt; &lt;td&gt;&lt;input type="radio" name="returnType" value="remove"&gt;Remove&lt;br&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&amp;nbsp&lt;/th&gt; &lt;td&gt;&amp;nbsp&lt;/th&gt; &lt;/tr&gt; &lt;tr&gt; &lt;th&gt;SKU&lt;/th&gt; &lt;th&gt;Order ID&lt;/th&gt; &lt;th&gt;Quantity&lt;/th&gt; &lt;th&gt;Location&lt;/th&gt; &lt;/tr&gt; &lt;?php $numberofrow = 10;?&gt; &lt;!--create the for loop--&gt; &lt;?php for($counter = 1;$counter&lt;=$numberofrow;$counter++){ ?&gt; &lt;!--create 1 row for repeating--&gt; &lt;tr&gt; &lt;td&gt;&lt;input type="text" id="sku&lt;?php echo $counter;?&gt;"/&gt;&lt;/td&gt; &lt;td&gt;&lt;input type="text" id="order&lt;?php echo $counter;?&gt;"/&gt;&lt;/td&gt; &lt;td&gt;&lt;input type="text" id="reEnterQty&lt;?php echo $counter;?&gt;"/&gt;&lt;/td&gt; &lt;td&gt; &lt;select id="reEnterLocation&lt;?php echo $counter;?&gt;" name="reEnterLocation" class="Location"&gt; &lt;option value=""&gt;--Select Location--&lt;/option&gt; &lt;?php $query = "SELECT location_id, location FROM location ORDER BY location"; $result = $conn-&gt;query($query); while ($row = $result-&gt;fetch_assoc()) { echo '&lt;option value="' . $row['location_id'] . '" &gt;' . $row['location'] . '&lt;/option&gt;'; } ?&gt; &lt;/select&gt; &lt;/td&gt; &lt;/tr&gt; &lt;?php }?&gt; &lt;/table&gt; &lt;br/&gt; &lt;br/&gt; &lt;input type="button" id="reEnterSKU" value="Process Returns" /&gt; &lt;br/&gt; &lt;div id="returnsNotice"&gt;&lt;/div&gt; &lt;/div&gt; </code></pre> <p>The js to handle this is:</p> <pre><code> $("#reEnterSKU").on('click', function() { if($("input[name=returnType]:checked").val() == "remove"){ //executes when Remove radio button is checked var arr = {}; var counter = 0; for(var x=1;x&lt;11;x++) { if($("#sku"+x).val() != "" &amp;&amp; $("#order"+x).val() != ""){ arr[x] ={sku: $("#sku"+x).val(), order: $("#order"+x).val(), quantity: $("#reEnterQty"+x).val()}; } } $.ajax({ type: "POST", async:false, url: "invReturnsRemove.php", dataType: "json", data: {data: JSON.stringify(arr)},//({sku: $("#sku"+x).val(), order: $("#order"+x).val(), quantity: quantity}), success: function(data){ } }); $("#returnsNotice").html("&lt;h3&gt;" + x + " return(s) removed.&lt;/h3&gt;"); </code></pre> <p>invReturnsRemove.php is:</p> <pre><code> &lt;?php require_once ('../db.php'); $conn = db_connect(); $n=0; $data =json_decode($_POST['data'], true); foreach($data as $value){ $conn-&gt;query("UPDATE order_sold SET quantity = (quantity - {$data[$n]['quantity']}) WHERE sku = {$data[$n]['sku']} AND order_id = {$data[$n]['order']}"); $n++;} $conn-&gt;close(); ?&gt; </code></pre> <p>The issue is that when I try to run it, even with only one record to remove, it runs hundreds of times. Any idea what I may be doing wrong, and how to fix it?</p>
 

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