Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You don't need to build the json, just build the data array and send it with <a href="http://api.jquery.com/jQuery.post/" rel="nofollow"><code>.post</code></a>, <a href="http://api.jquery.com/jQuery.get/" rel="nofollow"><code>.get</code></a> or <a href="http://api.jquery.com/jQuery.ajax/" rel="nofollow"><code>.ajax</code></a>. jQuery will take care of encoding the array for you:</p> <pre><code>var data = {}; for (var i = 0; i &lt; 3; ++i) { data['field' + i] = 'value' + i; } $.post ('http://mysite.com/page', data, function() { alert('succes!'); }); </code></pre> <p>Your server-side code's <code>$_POST</code> array will containing:</p> <pre><code>array( 'field1' =&gt; 'value1', 'field2' =&gt; 'value2', 'field3' =&gt; 'value3'); </code></pre> <p>For your example, I would reconsider sending the data as a string and instead send the data as well-formated key/value pairs. Your server-side code can more easily decide what to do with it, and your JS won't require a rewrite if the server-side code needs the string to be built differently.</p> <pre><code>$.ajax ({ type: "POST", url: "order.php", data: { supp_short_code: $('.supp_short_code').text(), project_ref: $('.project_ref').text(), om_part_no: $('.om_part_no').text(), description: $('.description').text(), cost_of_items: $('.cost_of_items').text(), cost_total: $('.cost_total').text() } //... }); </code></pre> <h3>Update</h3> <p>You could reduce the amount of typing by throwing your field names into an array, and appending the class name of any fields you want to include in the data array. Then loop over each of your table rows and extract the values:</p> <pre><code>var fields = [ 'supp_short_code', 'project_ref', 'om_part_no', 'description', 'cost_of_items', 'cost_total']; var data = []; // loop over each row $('#my-table tr').each(function (i, tr) { // extract the fields from this row var row = {}; for (var f = 0; f &lt; fields.length; ++f) { row[fields[f]] = $(tr).find('.' + fields[f]).val(); } // append row data to data array data.push(row); }); </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