Thursday, January 1, 2009

jQuery and AJAX

I'm trying to get some AJAX calls into a page I'm creating. jQuery is popular at work, so I decided to look into it. It's turns out to be pretty interesting. For example:
$(document).ready(function() {
// generate markup
$("#rating").append("Please rate: ");

for ( var i = 1; i <= 5; i++ ) $("#rating").append("
" + i + " ");

// add markup to container and apply click handlers to anchors
$("#rating a").click(function(e){
// send request
$.post("rate.php", {rating: $(this).html()}, function(xml) {
// format and output result
$("#rating").html(
"Thanks for rating, current average: " +
$("average", xml).text() +
", number of votes: " +
$("count", xml).text()
);
});

// stop normal link click
return false;
});
});



This code allows me to append some numerical hyperlinks to the end of a "ratings" id. Each hyperlink click ties to a separate page and sends "this", which I think is the html inside the hyperlink and returns a matching xml segment or page (I'm not sure). jQuery then extracts the info from the correct node and appends that as html. I need to figure out more.
Old post about Python (originally posted May 9, 2008)

I wrote a 12-lines python script that returns any number of any-length random strings to use as passwords for a web app I wrote for work. 12 lines! My php code goes on forever ...

I can't say the same thing about the Django web framework that goes with Python, mainly because I haven't had a new project to try.

The weekend starts soon ...