Sending HTML Form Multiple ` is normally submitted (without AJAX). When forms are submitted, each input's value is submitted as `key=value`, where the `key` is the `name` attribute of the element, and the `value` is the current value of the element. With a multiple ``, it replicates that for every value selected. – Ian Feb 08 '13 at 16:00

  • Thanks for the quick response. Makes total sense now to use $_POST['categories']. Unfortunately though it doesn't seem to be working yet with or without the extra brackets. I did notice in the jsFiddle you sent that when I changed the values in the select box I still got the same query string. – denvdancsk Feb 08 '13 at 16:19
  • @kyzer1978 Hmm that's weird. When I run the fiddle, I get all values selected. So say you click on the first option - it alerts `key=value`. Then hold control and click another - it should alert `key=value&key=value2`. It does for me at least. Here's a fiddle that alerts something less jumbled: http://jsfiddle.net/svYwL/1/ – Ian Feb 08 '13 at 16:30
  • @kyzer1978 I'm not the most fluent in PHP, but according to this post ( http://stackoverflow.com/questions/2407284/how-to-get-multiple-selected-values-of-select-box-in-php ), it says in order to access the values on the server, the name should be "categories[]" (with the []), and then access it like `$_POST["categories"]` (without the []). I would think since you use `print_r`, it would work just fine. – Ian Feb 08 '13 at 16:34
  • Yep, you're right. You're function is working like it's supposed to but for some reason the server isn't reading it correctly. I can clearly see the data being sent the way it's supposed to when I check it in firebug. It looks like this: categories%5B%5D%3D0%26categories%5B%5D%3D1%26categories%5B%5D%3D2%26 when I click all three. That looks just like it should using percent encoding. I even tried removing the encodeURIComponent function and got this: categories[]=0&categories[]=1&categories[]=2 Weird. Got to be something on my server end that I am not doing right. – denvdancsk Feb 08 '13 at 17:39
  • @kyzer1978 Right, the `encodeURIComponent` is just useful for the "correct" encoding for transmitting data through HTTP. Is there any other important PHP code you have, that you could include in your original question? Also, would you be able to debug the PHP and see what the full contents of `$_POST` are? I don't know what PHP code would do this, but some way of seeing what is all in `$_POST`? – Ian Feb 08 '13 at 17:43
  • There's something going on with the encryption. The best solution seemed to be Jquery and then setting the appropriate request header: setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); I really wish I could figure out why just using javascript didn't work but if Jquery works then I'll just go that route. – denvdancsk Feb 08 '13 at 19:24
  • @kyzer1978 Hey, whatever works for you! jQuery really shouldn't be required for this. I'm not sure what jQuery you ended up using, but using `.serialize()` does the same thing as I posted for generating the querystring. I should've recognized the need for the request header, I forgot about that with POST – Ian Feb 08 '13 at 19:32
  • 1
    @kyzer1978 Ahh I see what `.serialize` is doing differently. It doesn't seem to be encoding `=` and `&`. So I updated my answer to only encode the `key` and `value` in the pairs and that's it. Yet another dumb mistake by me. Hopefully that would fix it. I could've sworn it was okay to just call `encodeURIComponent` on the final querystring you were going to pass. – Ian Feb 08 '13 at 19:40
  • 4

    You can do something like this,

    <select multiple name="categories[]" onclick="sendCategories(this)">
    

    And Make AJAX using JQuery,

    function sendCategories(sel){
        var values = $(select).serialize();
        console.log (values);       // See if you get the serialized data in console.
    
        $.ajax({
            type: 'POST',
            url: "http://www.mysite.com/update_categories.php"
            data: values,
            success: function (data) {
                document.getElementById("my_div").innerHTML = data;
            }
        });
    }
    

    And FYI, Netscape event binding model is deprecated, you could use the cross browser event binding like this

    Salman
    • 9,299
    • 6
    • 40
    • 73
    • Thanks for the response. I tried it but it didn't work. Is there another step I have to do on the server end once the values are sent? For instance, do I have to unserialize the values first before it will work? – denvdancsk Feb 08 '13 at 15:42
    • I tried both print_r($_POST['values']); and also print_r(unserialize($_POST['values'])); but I didn't get anything. – denvdancsk Feb 08 '13 at 15:58
    • 1
      @kyzer1978 See if you get the values in `console.log(values)`, if yes, then you should see how you are sending the values to the server. Try the edited code.. – Salman Feb 08 '13 at 18:11
    • Jquery did the trick! I did have to set the RequestHeader first though. I added xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); to my original code and used the serialize() function and boom! Problem solved! – denvdancsk Feb 08 '13 at 19:23
    1

    You can implement the solution however you would like using JS string and array functions. Effectively, the string you need to send to Apache should contain a pattern like:

    xxx[]=a&xxx[]=b&xxx[]=c

    where the SELECT element's name is xxx[] in your form and a, b, and c are three values the user selected.

    So yes, you are repeating a key name as many times as the user selected a different option in the SELECT.

    In JS you can use an array of selected options:

    selected_options.join("&xxx[]=") to produce that pattern.
    
    almightyGOSU
    • 3,731
    • 6
    • 31
    • 41
    0

    jQuery should make this easier for you. Calling .val() on a wrapped select returns an array of the selected values. You just have to post these to the server:

    HTML:

    <html>
        <body>
            <form>
                <select name="mySelect" multiple="on">
                    <option value="1">Uno</option>
                    <option value="2">Dos</option>
                    <option value="3">Tres</option>
                </select>
            </form>
            <input id="submitButton" type="button" value="Click To Submit"/>
        </body>
    </html>
    

    JavaScript:

    $(function() {
        $('#submitButton').click(function() {
            var valuesArray = $('select[name=mySelect]').val()
            $.ajax({
                type: 'POST',
                url: '/path/to/php', // your php url would go here
                data: { mySelect: valuesArray }
            }).done(function(msg) {
                // parse response from msg
            });
        });
    });
    
    mockaroodev
    • 2,031
    • 1
    • 20
    • 24
    • downvoter please comment what is wrong here. I do not see anything wrong in this. – Ashwin Feb 08 '13 at 05:33
    • Thanks but I am actually trying to generate dynamic html code before the entire form is submitted to the server so this doesn't solve the problem. I basically want to reveal different options in the form whenever the user clicks on different categories in the select box before the form is sent to the server via AJAX. – denvdancsk Feb 08 '13 at 15:47