job kwemoi
job kwemoi

Reputation: 65

Jquery ui sortable does not create array

I wonder why my jquery UI does not create an array that i can parse to php, this is my list

<ul id="sortable">
              <li id="firstname" class="ui-state-default"><span class="ui-icon ui-icon-arrowthick-2-n-s"></span>firstname</li>
              <li id="lastname" class="ui-state-default"><span class="ui-icon ui-icon-arrowthick-2-n-s"></span>lastname</li>
              <li id="title" class="ui-state-default"><span class="ui-icon ui-icon-arrowthick-2-n-s"></span>title</li>
              <li id="book_title" class="ui-state-default"><span class="ui-icon ui-icon-arrowthick-2-n-s"></span>book_title</li>
            </ul>

And here is jQuery

 <script>
$(function() {
    $( "#sortable" ).sortable({
    placeholder: "ui-state-highlight",
    opacity: 0.6,
   update: function(event, ui) {
    var info = $(this).sortable("serialize");
    alert(info);
    $.ajax({
        type: "POST",
        url: "home.php",
        data: info,
        context: document.body,
        success: function(){
        }
  });

}
});
$( "#sortable" ).disableSelection();
});
</script>

thi is the result that alert (info ) gives

 book[]=title

I want to be able to post the array to php so that the user can change the order of the output according to how he changes it in the list. Someone help

edited from here, my home.php file

  <?php 
header("Content-type: text/xml"); 
include_once("config.php");

parse_str($_POST['data'], $order);
echo $order; 

$query = "SELECT    `author`.`surname`,`author`.`firstname`,`publication`.`title`,`book`.`book_title` FROM author, book, publication "; 
$resultID = mysql_query($query, $conn) or die("Data not found."); 

$xml_output = "<?xml version=\"1.0\"?>\n"; 
$xml_output .= "<entries>\n"; 

for($x = 0 ; $x < mysql_num_rows($resultID) ; $x++){ 
$row = mysql_fetch_assoc($resultID); 
$xml_output .= "\t<entry>\n"; 
foreach($order as $col){
    $xml_output = "\t\t<$col>" . $row[$col] . "</$col\n"; 
}    
$xml_output .= "\t</entry>\n"; 
} 
$xml_output .= "</entries>"; 
echo $xml_output;  
?> 

Upvotes: 0

Views: 394

Answers (1)

raina77ow
raina77ow

Reputation: 106453

It's said in sortable.serialize documentation:

...It works by default by looking at the id of each item in the format "setname_number", and it spits out a hash like "setname[]=number&setname[]=number".

But there's only one id in your set of elements that is matching this pattern - it's book_title (setname is 'book', number is 'title'). All the others do not match this pattern, and are just ignored, I suppose.

The easiest approach to this is just to change your ids into this:

post_firstname
post_lastname
post_title
post_booktitle

You can still use the original values, but then you'll have to supply your own expression pattern (and probably key, if the pattern will look like /()(.+)/) as serialize's options. Or you can consider another alternative - toArray method.

Upvotes: 3

Related Questions