tags:

views:

286

answers:

2

Hi, I am new to Jquery and am trying to store the location and size of the elements on a page which are being sorted using the Jquery UI plugin. I want the user to "save" the current layout, and the data to be stored in a database using php/mysql so when they re-visit the elements will be the same size and location?

 <script type="text/javascript">
    $(function() 
    {
     $("#sortable").sortable({
      revert: true,
      axis: 'y',
      opacity: 0.9,
      tolerance: 'pointer',

     });

     $("#draggable").draggable({
        connectToSortable: '#sortable',
      helper: 'clone',
      revert: 'invalid'   
     }) 
     $("#sortable").resizable({})  
    });

    </script>

Above is part of my script which basically allows boxes to be sorted and dragged using a list.

Many Thanks.

EDIT:

<body>

<div class="demo">

<ul id="sortable">
    <li class="ui-state-default">Item 1</li>
    <li class="ui-state-default">Item 2</li>
    <li class="ui-state-default">Item 3</li>
</ul>

</div>

I am trying to use div tags alone without having to get them in a list also? Without having to include the divs inside a list.

Thanks again.

+1  A: 

Make an Ajax call in the draggable callback function.

This is because from the callback you can get the current co-ordinates, and you will be able to save them to the database.


Here's an example :

 $("#draggable").draggable({
                connectToSortable: '#sortable',
                helper: 'clone',
                revert: 'invalid',
                stop : function (event, ui) {
                    //callback function
                    $.post("savepos.php", 
                        { 
                            //parameters to send to your ajax page
                            top: ui.position.top, 
                            left : ui.position.left 
                        }  
                    );
                }
            });
Andreas Grech
Here's an article talking about combining php and ajax : http://www.ibm.com/developerworks/opensource/library/os-php-jquery-ajax/index.html
Andreas Grech
Hi thanks, from the php script what will I be 'getting', will I use the GET Function? e.g. GET['top'] etc, is that how they are sent? Thanks
Elliott
from the php, take the variables like such : $_POST['top']
Andreas Grech
A: 

sortable has convenience methods for just this scenario called serialize (see the docs). serialize produces a query string that can be used in an AJAX request; you might use it something like this:

$('#sortable').sortable(
{
    stop: function(event, ui)
    {
        $.post('save.php', $('#sortable').serialize());
    }
});

Then it's just a question of implementing save.php to support this process. The example query string given in the docs (foo[]=1&foo[]=5&foo[]=2) would produce a $_POST array with:

array(1) {
  ["foo"]=>
  array(3) {
    [0]=>
    string(1) "1"
    [1]=>
    string(1) "5"
    [2]=>
    string(1) "2"
  }
}
Will Vousden