开发者

How to send an HTML array trought AJAX

I have these checkboxes :

<input type="checkbox" name="style[]" value="1" checked="checked" />
<input type="checkbox" name="style[]" value="2" />
<input type="checkbox" name="style[]" value="3" checked="checked" />
<input type="checkbox" name="style[]" value="4" checked="checked" />
<input type="checkbox" name="style[]" value="5" />

and this jquery handler :

<script type="text/javascript">
    $(document).ready(function() {
        $('.linkStyle').click(function(e) {
            // 
        });                 
    });
</script>

so, when I click on a checkbox, I'd like to send the array style[] to the server trought AJAX (with the actual values).

How can I do it?

EDIT

Tried :

$.post("list/browse_ajax.php", {id:"browseUpdate", actualArtist:browseArtist, actualEvent:browseEvent, actualData:browseData, style:$('#browseTableStyle:checkbox').serialize()},
    function(data) {
        $("#browseList").html(data);
    }
);  

but the style[] is not send...

EDIT 2 - Problem with serialize

This is the whole code :

<form id="browseForm">
    <div class="wideinfo">
        <div class="content">
            <div class="tableList1">
                Style
            </div>  

            <div class="tableList2">
                <span><input type="checkbox" name="style[]" value="Style1" checked="checked" /> Style1</span>
                <span><input type="checkbox" name="style[]" value="Style2" /> Style2</span>
                <span><input type="checkbox" name="style[]" value="Style3" checked="checked" /> Style3</span>
                <span>&l开发者_StackOverflowt;input type="checkbox" name="style[]" value="Style4" checked="checked" /> Style4</span>
                <span><input type="checkbox" name="style[]" value="Style2" /> Style5</span>
            </div>
        </div>
    </div>
</form>

but with :

style:$('#browseForm').serialize()

it sends this (checked on Fiddler)

style: style%5B%5D=Style1&style%5B%5D=Style3&style%5B%5D=Style4

not :

style[]: Style1
style[]: Style3
style[]: Style4

The last is a real/comprensive HTML array...


What about :

$.post('my-url', $(':checkbox').serialize(), function(data, textStatus, jqXHR){
    // my callback body
});

EDIT: Actually the serialize function only works on form (and thus serialize form fields). So you have to wrap your fields in a form. See bellow.


When you finnaly have your string submitted style%5B%5D=Style1&style%5B%5D=Style3&style%5B%5D=Style4 you can retrieve the comprehensive array like that:

parse_str($_POST['style'], $data);

The the $data variable contains:

array(
    "style" => array(
        0 => "Style1",
        1 => "Style3",
        2 => "Style4",
    )
)

Does it solve your problem ?


First, your callback does not seem to be consistent :

function(data) {
    $("#browseList").html(msg);
}

You might want to say :

function(data) {
    $("#browseList").html(data);
}

Also have you check firebug or your explorer javascript debuger to see if there is any error. Also can you show us the actual submited data. Finnaly what is the result of a call to :

$(':checkbox').serialize()


Yes, this should work.

style:$('#browseForm').serialize()

At the end I strongly advise you to inclue all your post data in the form so that you can simply do:

$.post('my-url', $('form').serialize(), function(data){
    $("#browseList").html(data);
});

It is cleaner, straightforward and easier to maintain. After that your PHP script should handle the rest.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜