<p>I want to write an <code>onClick</code> event which submits a form several times, iterating through selected items in a multi-select field, submitting once for each.  </p>

<p><strong>How do I code the loop?</strong></p>

<p>I'm working in Ruby on Rails and using <code>remote_function()</code> to generate the JavaScript for the ajax call.</p>

[{'answer_id': 61384, 'author': 'martinatime', 'author_id': 1353, 'author_profile': 'https://Stackoverflow.com/users/1353', 'pm_score': 3, 'selected': False, 'text': "<p>My quick answer (as I've not coded it yet) would be to create another function that creates a POST using XMLHTTPRequest and the specific parameters for a single call. Then inside your onClick() handler call that function as you loop through your selected items.</p>\n\n<p>I would suggest that you do a Proof of Concept just using a dummy HTML page and javascript and then try to figure out how to get it to work in RoR.</p>\n\n<p>Also, why are you attempting to make the multiple calls from the browser as opposed to handling the looping conditions in the RoR controller?</p>\n"}, {'answer_id': 61651, 'author': 'Orion Edwards', 'author_id': 234, 'author_profile': 'https://Stackoverflow.com/users/234', 'pm_score': 2, 'selected': False, 'text': '<p>You\'d have to manually write some javascript. Rails\' generators won\'t do something this complex for you.</p>\n\n<p>Prototype.js will do almost all of the heavy lifting for you though. Off the top of my head, the code would look like this: (UNTESTED)</p>\n\n<pre><code>&lt;%= javascript_include_tag \'prototype\' %&gt;\n\n&lt;form id="my-form"&gt;\n  &lt;input type="text" name="username" /&gt;\n\n  &lt;select multiple="true" id="select-box"&gt;\n    &lt;option value="1"&gt;First&lt;/option&gt;\n    &lt;option value="2"&gt;Second&lt;/option&gt;\n    &lt;option value="3"&gt;Third&lt;/option&gt;\n    &lt;option value="4"&gt;Fourth&lt;/option&gt;\n  &lt;/select&gt;\n&lt;/form&gt;\n\n&lt;script type="text/javascript" language="javascript"&gt;\nsubmitFormMultipleTimes = function() {\n  $F(\'select-box\').each(function(selectedItemValue){\n    new Ajax.Request(\'/somewhere?val=\'+selectedItemValue, \n      {method: \'POST\', postBody: Form.serialize(\'my-form\')});\n  });\n}\n&lt;/script&gt;\n\n&lt;a href="#" onclick="submitFormMultipleTimes(); return false;"&gt;Clicky Clicky&lt;/a&gt;\n</code></pre>\n\n<p>Note:</p>\n\n<ul>\n<li><p>Using Prototype\'s <code>$F()</code> method to get the selected item values. It returns an array for multiple-select boxes</p></li>\n<li><p>Using <code>Ajax.Request</code> to send the data to the server as a <code>POST</code>.<br>\nTo the server, this looks exactly the same as just submitting a normal form</p></li>\n<li><p>Using <code>Form.serialize</code> to get the data out of the form and stick it in the request\'s body.<br>\nThis is the exact same data that would get sent if you submitted the form normally</p></li>\n</ul>\n'}, {'answer_id': 79004, 'author': 'Dave Smylie', 'author_id': 1505600, 'author_profile': 'https://Stackoverflow.com/users/1505600', 'pm_score': 1, 'selected': False, 'text': "<p>Unless you're modifying the browser DOM, I can't think of a reason that you would want to do this. (But without knowing fully what you're trying to do, I could be wrong in this case =)</p>\n\n<p>You should be able to send back data from mulitple objects (even nested complex objects in your form) in just one POST.</p>\n\n<p>Chances are the rails code will be a lot less complex, easier to write (and easier to debug!) than any javascript you come up with. </p>\n\n<p>If you need to update different parts of the page depending on what the user has selected, you can still make multiple updates to the DOM via RJS in your render :update block, so that shouldn't be an issue.</p>\n\n<p>You'll also have the (large) benefit of only one server round-trip instead of the multiple trips you would need using multiple POSTS.</p>\n"}]
['https://Stackoverflow.com/questions/61372', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3764/']