views:

321

answers:

2

Let's assume we have an HTML multi-select.

How to prepare an ajax request URL with the selected values from the multi-select. I'm using YUI dataTable, so I do not think I can use form serialization (but would not even want to even if I could)? The URL generated should be the same as with a normal form submission with page refresh.

I did this but was hoping a better way with YUI was possible:

function createURLFromMultiSelect(ob, name) { 
  var url = "";
  for (var i = 0; i < ob.options.length; i++) {
   if (ob.options[ i ].selected) {
    url += "&" + name + "[]=" + ob.options[ i ].value;
   }  
  }
  return url;
}
A: 
name=value1&name=value2&name=value3
David Dorward
Well, OK. This is just what I want to do. How do I do it elegantly?
JB Hurteaux
JB Hurteaux
No, it shouldn't. If you are using PHP and using PHP's built in form parsing library, then the name must end with the characters "[]" but those characters are part of the name, not separate to it.
David Dorward
yes ok. Depends on the name of the input and how you build your URL though. I understand you're assuming the input name to include the [].
JB Hurteaux
If the input name doesn't include the [] part in the name (and you use PHP) then it isn't going to work when the non-JS fallback hits.
David Dorward
I think you're correct, it belongs in the element name, and I should not manually add it in the URL (even though I do not handle progressive enhancement or unobtrusive javascript for now). Thanks.
JB Hurteaux
A: 

The name of your select can should end with []. For example, <select name="items[]">
Then in your php code, you simply access $_POST['items'][$n]; or count($_POST['items']); or whatever you want to do with the array.

--Dave

the Hampster
I think you have misunderstood my use case. I'm not submitting a web 1.0 form, I want to have a way to manually construct the URL for sending an ajax request. (or you are relying are form serialization, which I kind of eliminated from the solution).
JB Hurteaux