Note that there are some explanatory texts on larger screens.

plurals
  1. POSubmitting multiple forms with one submit
    primarykey
    data
    text
    <p>In my photo portfolio admin page, I've built a tool to caption, keyword and credit each photo. Until now, I've dynamically listed multiple forms, each containing a submit button. With 20+ photos/forms on the page, this can be a boring job and was hoping to build on this by adding a button at the bottom of the page to submit all.</p> <p>I went ahead and built what I thought was a good approach and looped through all the forms one-by-one and posted the data to a PHP page using AJAX. This worked fine, but like I mentioned, with 20+ photos, it's running 20 AJAX requests and 20 SQL updates, and now feel this is a poor effort.</p> <p>I'm now looking at other ways that involve looping through each form, creating an array or object, and sending it in one request and have one nifty SQL query to update all rows at once.</p> <p>This is where I'm at now. I'm struggling to get the form fields into a readable and usable object for my PHP to read.</p> <p><strong>MY FORM STRUCTURE</strong></p> <pre><code>&lt;form name="12344"&gt; &lt;div class="input_wrap"&gt; &lt;textarea id="12344_caption"&gt;&lt;/textarea&gt; &lt;/div&gt; &lt;div class="input_wrap"&gt; &lt;input type="text" id="12344_keywords" /&gt; &lt;/div&gt; &lt;div class="input_wrap"&gt; &lt;input type="text" id="12344_credit" /&gt; &lt;/div&gt; &lt;div class="input_wrap"&gt; &lt;input type="text" id="12344_credit_url" /&gt; &lt;/div&gt; &lt;/form&gt; &lt;form name="12345"&gt; &lt;div class="input_wrap"&gt; &lt;textarea id="12345_caption"&gt;&lt;/textarea&gt; &lt;/div&gt; &lt;div class="input_wrap"&gt; &lt;input type="text" id="12345_keywords" /&gt; &lt;/div&gt; &lt;div class="input_wrap"&gt; &lt;input type="text" id="12345_credit" /&gt; &lt;/div&gt; &lt;div class="input_wrap"&gt; &lt;input type="text" id="12345_credit_url" /&gt; &lt;/div&gt; &lt;/form&gt; </code></pre> <p><strong>MY JQUERY SO FAR</strong></p> <pre><code>var photo_annotations = {}; $('form').each(function(i) { var id = $(this).attr('id'); var caption = $('#'+id+'_caption').val(); var keywords = $('#'+id+'_keywords').val(); var credit = $('#'+id+'_credit').val(); var credit_url = $('#'+id+'_credit_url').val(); // create object to post photo_annotations[id].push({}); (help) } $.ajax({ type: 'POST', url: 'ajax/save-annotations.php', data: { json: JSON.stringify(photo_annotations) } }); </code></pre> <p>This is the kind of data I'd like to construct:</p> <pre><code>photo_annotations = { "12344": { "caption": "This is a caption for the photo ID 12344.", "keywords": "Keyword1, Keyword2, Keyword3", "credit": "John Doe", "credit_url": "http://www.johndoe.com" }, "12345": { "caption": "This is a caption for the photo ID 12345.", "keywords": "Keyword4, Keyword5, Keyword6", "credit": "Joe Bloggs", "credit_url": "http://www.joebloggs.com" } } </code></pre> <p>I'm struggling to get my form fields into JSON correctly, in the format I showed you above. I hope that somebody could show me how to achieve this format.</p>
    singulars
    1. This table or related slice is empty.
    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.
    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