开发者

jQuery serialize value to PHP

I am a beginner in integrating jQuery and PHP. I tried using the nestedsortable plugin of jquery and successfully integrated it to my kohana 2.3.4 framework. My problem is how can I pass the serialize array value of jquery to php? Here is a snippet of my code.

In my View:

<ol class="sortable">
    <li id="list_1"><div>Item 1</div>
    <li id="list_2"><div>Item 2</div>
        <ol>
            <li id="list_3"><div>Sub Item 1.1</div>
            <li id="list_4"><div>Sub Item 1.2</div>
            <li id="list_5"><div>Sub Item 1.3</div>
            <li id="list_6">开发者_StackOverflow中文版;<div>Sub Item 1.4</div>
        </ol>
</ol>

<script>

$(document).ready(function(){

    $('ol.sortable').nestedSortable({
        disableNesting: 'no-nest',
        forcePlaceholderSize: true,
        handle: 'div',
        helper: 'clone',
        items: 'li',
        opacity: .6,
        placeholder: 'placeholder',
        tabSize: 25,
        tolerance: 'pointer',
        toleranceElement: '> div'
    });

    $('#serialize').click(function(){
        serialized = $('ol.sortable').nestedSortable('serialize');
        $('#serializeOutput').text(serialized);
    })
});


Via an AJAX Call: "serialized" = your serialized data.

$.ajax({
   type: "POST",
   url: "myPhpScript.php",
   data: serialized
 });

More on this in the jQuery Docs

In your PHP file, you can access these variables through $_POST.

$myVar = $_POST['myVar']; 

I don't know exactly how your serialized variables are called, but you can always "var_dump($_POST)" to see the contents of the POST data, and figure it out from there.


Stick all of this into your $(document).ready(function()/

$('ol.sortable').nestedSortable({
    disableNesting: 'no-nest',
    forcePlaceholderSize: true,
    handle: 'div',
    helper: 'clone',
    items: 'li',
    maxLevels: 10,
    opacity: .6,
    placeholder: 'placeholder',
    revert: 250,
    tabSize: 25,
    tolerance: 'pointer',
    toleranceElement: '> div'
});

$('#serialize').click(function(){
        serialized = $('ol.sortable').nestedSortable('serialize');
        $('#serializeOutput').text(serialized+'\n\n');
        $.post({
                type: "POST",
                url: "script.php",
                data: serialized
    });
})

Assuming that you have your serialize button handy, simply click the button and the data will be posted. Notice that I just added .post instead of .ajax. From my tests (I am using Django, not php), both .post and .ajax work fine.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜