views:

84

answers:

1

I have a page (page1.php) where I am using a select box to load in another page (page2.php) into a DIV. Inside page2.php there is a UL that loads data from a database (via PHP) into LIs and the are sortable.

My problem is, when I load page2.php by itself, it serializes fine. However, when page2.php is loaded via .load() into page1.php, it doesn't serialize at all and I get undefined.

Here is the important code, again this works fine by itself, but not when this page is loaded in via the .load() function

 <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"&gt;&lt;/script&gt; 
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.min.js"&gt;&lt;/script&gt; 
<style> 
    #thelist { list-style-type: none; margin: 0; padding: 0; width:700px; }
    #thelist li { margin: 3px 3px 3px 0; padding: 1px; float: left; width: 200px; height: 150px; }
    </style> 
<ul id="thelist">
<li style="margin-bottom:5px;" name='listItem_1' id='listItem_1'>
    test1
</li>
<li style="margin-bottom:5px;" name='listItem_2' id='listItem_2'>
    test2
</li>
<script type="text/javascript"> 
    $(function() {
        $("#thelist").sortable({ 
            update : function () { 
              var order = $('#thelist').sortable('serialize'); 
              alert(order); // This alerts "undefined" when page2.php is loaded into page1.php via .load();
              $("#info").load("reorder_slides.php?"+order); 
        }});
    });
</script> 

This is the new code I am running, still to no avail.

<script>
    $('#edit_service_date').change(function() {
        // $(this).val()
        $('#editService').slideUp("slow",function(){
            $('#thelist').load('page2.php', {id: $('#edit_service_date').val()}, function(){
                $("#thelist").sortable({ 
                    update : function () { 
                      var order = $('#thelist').sortable('serialize'); 
                      alert(order); // This alerts "undefined" when page2.php is loaded into page1.php via .load();
                      $("#info").load("reorder_slides.php?"+order); 
                }});
                if($('#edit_service_date').val()!="none"){
                    $('#editService').slideDown("slow");
                }
            });
        });
    });
</script>
A: 

If everything you posted above is being brought into another page via .load(), then I see (at least) two problems:

  1. You're loading jQuery and jQuery UI twice: once in the outer page, and once in the inner page loaded via ajax. There's no need.
  2. You're expecting $(function(){}) to fire after being loaded into the "inner" page within the div. $(function(){}) is a synonym for $(document).ready( function(){} ), and in fact the ready event has already fired (when the outer page DOM became ready). It won't do anything here.

You should try triggering the .sortable() stuff inside the callback of the .load() you're using to bring the inner document into the div:

/* on page1.php */
$('#yourdiv').load( 'page2.php', function(){
    $('#thelist').sortable( /* arguments */ );
});
Ken Redler
Ken, thanks for your input.I tried what you suggested, but to no avail. Also, I'd like to point out that I don't want to serialize when the page loads, but rather when the sortable function is updated. I have updated my first post with the new code I am running.
Nick Barone
Nick, in your revised code it looks like you have an element with the id `thelist` (the container -- is this supposed to be a `div`?), to which you apply `.load()`...and then in the callback you apply `.sortable()` to that same id. So you're loading into an id, and in the loaded content you have a list with that same id -- and then you're trying to make that ambiguous id sortable?
Ken Redler
Ken, I toyed around with loading LIs into the UL (#thelist) to see if keeping the UL on the main page would make it serialize. This method didn't work, but as of right now I'm loading the whole UL into a div, #editService
Nick Barone
Ken, I went back and reread your first post. After removing the calls to jquery and jquery-ui in the second page, it works fine... very odd but I'm a little upset I wasted a couple hours on that.I appreciate your willingness to help, and if I could vote your anwer up I would do so!
Nick Barone
Nick, glad to hear you got it sorted out.
Ken Redler