Sortable loading

Sortable loading

I've got a sortable and need to know how to delay the loading of the positions.
Here is the code:

  1. $(document).ready(function(){
            $("#sortable").sortable();
            $("#sortable").disableSelection();
      $.ajax({
              type: "GET",
              url : "userFileFormat.php",
              data: "action=get",
              success: function(res) {
                var sortable_ = $("#sortable");
                var dragstr = res;
                if (dragstr == '') return;
                $.each(dragstr.split(','),function(i,id) {
                     $("#"+id).appendTo(sortable_);
                   });
              },
              error: function(xhr, ajaxOptions, thrownError) {
                alert(xhr.status);
                alert(thrownError);
              }
            });   
        $("#sortable" ).bind("sortstop", function(event, ui) {
            var result = $("#sortable" ).sortable('toArray');
            $.ajax({ type: "GET",
                     url : "userFileFormat.php",
                     data: "action=store&format="+result,
                     success: function(res) { alert(res) },
                     error: function(xhr, ajaxOptions, thrownError) {
                     alert(xhr.status);
                     alert(thrownError);
                   }
                 });
          });
      });
































Now, I use the first ajax call to load and order the sortable based on a string returned from userFileFormat.
This string represents the order that the items need to be in, i.e. 1,2,3,4,5,6,7 is the default order.

Here is how the sortable is defined, it is contained in a hidden div:
  1. <div id="config_tool" style="display: none">
      <ul id="sortable">
        <li id="1" class="ui-state-default">Part #</li>
        <li id="2" class="ui-state-default">Alt Part #</li>
        <li id="3" class="ui-state-default">Condition</li>
        <li id="4" class="ui-state-default">Quantity</li>
        <li id="5" class="ui-state-default">Description</li>
        <li id="6" class="ui-state-default">Other Col 1</li>
        <li id="7" class="ui-state-default">Other Col 2</li>
      </ul> 
    </div>










Now, some customers have different formats and I want to show the sortable in the order of their format.
For instance, if their format is 1,5,4,3,6,2,7, the sortable should show in this order:
Part#,Description,Quantity,Condition,Other Col1,Alt Part #,OtherCol2

The main problem is that the userFileFormat.php REQUIRES the user to be logged in and when this is loaded, the User class has not yet been instantiated nor the user logged in. The ajax call to store the positions works just great because they won't be able to use it until they are already logged in.

How can I delay the loading of the positions and maybe call the ajax loading part in another javascript function. I currently show the div that contains the sortable in a javascript function and I should be able to make that ajax call at that time, but how?

Here's the code to the userFileFormat PHP script:
  1. <?php
      require_once "Smarty.class.php";
      require_once "libs/class/User.class.php";
      $user = new User;
      $user->loadUserSmarty();
      if (isset($_GET["action"])) {
     
        // we need to return the format for the user
        if ($_GET["action"] == 'get') {
          $result = $user->getUserFileFormat();
          echo $result;
        } //if ($_GET["action"] == 'get')
       
        // we need to store the user's format
        if ($_GET["action"] == 'store') {
          if (isset($_GET["format"])) {
            $format = $_GET["format"];
          } else {
            $format = '1,2,3,4,5,6,7'; // default format
          } // if (isset($_GET["format"]))
          $result = $user->storeUserFileFormat($format);
          if (!$result) {
            return false;
          } else {
            echo true;
          } // if (!$result)
        } // if ($_GET["action"] == 'store')
       
      } else {
        echo 'fail: No action';
      } // if (isset($_GET["action"]))
    ?>