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;
});
});
// 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.