Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9060941
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T15:21:39+00:00 2026-06-16T15:21:39+00:00

I have been playing around with the sortable() functionality and I have got it

  • 0

I have been playing around with the sortable() functionality and I have got it working fine on static DB results, however I have noticed that I can’t get it to work on AJAX-generated DB results. I feel it has something to do with how I call the sortable() function within the $(document).ready(function() however I am not 100% sure. I have searched this forum and other forums to see if anyone else has this problem but I haven’t found anything similar yet, so here’s my attempt to describe my problem:

I have a MySQL database called ‘db_objects’ which contains 1 table:

CREATE TABLE IF NOT EXISTS `tbl_objects` (
  `obj_id` int(11) NOT NULL AUTO_INCREMENT,
  `obj_name` varchar(50) DEFAULT NULL,
  `obj_type` int(11) DEFAULT NULL,
  PRIMARY KEY (`obj_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=10 ;

INSERT INTO `tbl_objects` (`obj_id`, `obj_name`, `obj_type`) VALUES
(1, 'Sam', 1),
(2, 'John', 1),
(3, 'Tom', 1),
(4, 'Bob', 1),
(5, 'Fluffy', 2),
(6, 'Paws', 2),
(7, 'Kitty', 2),
(8, 'Tibbles', 2),
(9, 'Mr. Meow', 2);

Now, I have a HTML page where I want to view the results on this table based on a drop-down box which filters by the obj_type column (1 = human names, 2 = feline names):

<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'><html xmlns='http://www.w3.org/1999/xhtml'><head><title>SORTABLE EXAMPLE</title>
<link rel='stylesheet' href='http://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css'>
<script src='http://code.jquery.com/jquery-1.8.3.js'></script>
<script src='http://code.jquery.com/ui/1.9.2/jquery-ui.js'></script>
<style>
div.div_container {
 background:#0F6;
 max-width:400px;
 padding: 10px 10px 10px 10px;
}
h2.h2_med_green { 
 display: inline;
 font-weight:normal;
 color:#090;
 font-size: 1.4em;
}
ul { 
 list-style: none;
 padding: 0px 0px 0px 0px;
 margin: 0px 0px 0px 0px;
}
li.li_sortable {
 cursor: hand; 
 cursor: pointer;
 border-bottom: 1px dotted #999999;
 background:#DDDDDD;
 height: 31px;
   line-height: 31px;
}
</style>
<script type='text/javascript'>
 $(document).ready(function() {
 //CALL SORTABLE FUNCTIONALITY
 $('#ul_sortable').sortable();
 //CALL AJAX RESULTS
 showAJAXResults(1);
 });
</script>
</head>
<body>
<div class="div_container">
    Click and drag each round to re-order.<br />Object Type:&nbsp;
    <select id="ajax_select_type">
     <option value="1">Human</option>
        <option value="2">Feline</option>
    </select>
    <div id="ajax_results"></div>
</div>
<script>
$("#ajax_select_type").change( function() {
 //CALL AJAX DBR
 showAJAXResults($(this).val());
});
function showAJAXResults(obj_type){

 //HTTP REQUEST CODE
 if (window.XMLHttpRequest){xmlhttp=new XMLHttpRequest();} // code for IE7+, Firefox, Chrome, Opera, Safari
 else{xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}// code for IE6, IE5    
 xmlhttp.onreadystatechange=function(){if (xmlhttp.readyState==4 && xmlhttp.status==200){document.getElementById("ajax_results").innerHTML=xmlhttp.responseText;}}

 //ASSEMBLE THE PAGE CALL VALUE
 var page_call_value = "ajax/ajax_page.php?t="+obj_type;

 //CALL THE AJAX PAGE
 xmlhttp.open("GET",page_call_value,true);
 xmlhttp.send();
} 
</script>
</body>
</html>

Finally, the above page links to the following PHP page which is called via AJAX:

<?php 
 //DETERMINE INPUT VALUES 
 $object_type=$_GET["t"]; 

 //CONNECT TO THE DATABASE 
 $con = mysql_connect("localhost","<USERNAME>","<PASSWORD>"); 
 mysql_select_db("db_objects", $con); 

 //GET SQL QUERY 
 $q_db_result = "SELECT DISTINCT obj_id, obj_name, obj_type 
 FROM tbl_objects 
 WHERE obj_type = '".$object_type."' 
 ORDER BY obj_name"; 

 //Execute the FULL_RESULT_QUERY QUERY 
 $sql_results = mysql_query($q_db_result) or die(mysql_error()); 

 //TURN THE RESULTS RETRIEVED INTO AN ARRAY 
 $results_array = array(); 
 while($row = mysql_fetch_assoc($sql_results)){ 
 $results_array[$row['obj_id']]['obj_name'] = $row['obj_name']; 
 $results_array[$row['obj_id']]['obj_type'] = $row['obj_type']; 
 } 

 //ASSEMBLE THE RESULTS 
 if(sizeof($results_array)>0){ 

 foreach($results_array as $obj_id => $result_details){ 
 //DECLARE VARIABLES 
 $obj_name=$result_details['obj_name']; 
 $obj_type=$result_details['obj_type']; 

 $return_value .= "<li class='li_sortable' value='".$obj_id."'><h2 class='h2_med_green'>".$obj_name."</h2></li>\n"; 
 } 

 //PUT THE UL TAGS AROUND THE HTML CODE 
 $return_value = "<ul id='ul_sortable'>\n".$return_value."</ul>\n"; 

 //PUT THE RESULTS IN THE INNER CONTAINER DIV 
 $return_value = "<div id='ajax_results_ic' class='ajax_results_ic_2'>".$return_value."</div>"; 

 }else{ 
 $return_value = "NO RESULTS RETURNED"; 
 } 

 //DISPLAY HTML CODE 
 echo $return_value; 

 //CLOSE DB CONNECTION 
 mysql_close($con); 
?>

Now, if you can get this working you will notice that the sortable() functionality does not work. As I mentioned before, I think it has something to do with the fact that the DB results are generated on-the-fly by AJAX, so trying to declare/call the sortable() functionality in the $(document).ready(function() won’t work, i.e.

$(document).ready(function() {
 $('#ul_sortable').sortable();
});

I’m not sure how to get around this – does anyone have any suggestions? Thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T15:21:40+00:00Added an answer on June 16, 2026 at 3:21 pm

    you can call the sortable() function after the reponseText is appended .. so that it gets the dynamically generated DOM elements…

    xmlhttp.onreadystatechange=function(){if (xmlhttp.readyState==4 && xmlhttp.status==200){document.getElementById("ajax_results").innerHTML=xmlhttp.responseText;$('#ul_sortable').sortable();}}  //here
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been playing around with a search control and i have noticed that
I have been playing around with the EF to see what it can handle.
I have been playing around in PHP with it and got something to work,
I have been playing around with modifiers with static method and came across a
I have been playing around with Threads and Tasks (.net 4) and noticed some
I have been playing around with some debugging and wrote some C code that
I have been playing around with haskell and I found out that if I
I have been playing around with indexes on MySQL (5.5.24, WinXP), but I can't
I have been playing around with this problem since yesterday. I can't seem to
I have been playing around with RavenDB (build 531) and I can't seem to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.