js

by nick on March 8th, 2010
No notes
Syntax: JavaScript
Show lines - Hide lines - Show in textbox - Download
/*---------------------------------------------------------------*/
/*---------------- JQuery code to handle Ajax -------------------*/
/*------------------- ©409523(2009/2010) ------------------------*/
/*---------------------------------------------------------------*/
 
$(function(){
	//Defined Variables to make Global
	var inputName    = $("#name");
	var inputComment = $("#comment");
	var ajaxLoader   = $("#ajaxLoader");
	var commentsList = $(".commentContents > ul");
 
	/*- Functions for comments page -*/
 
	//Update the Comments on the page
	function updateComments(){
		commentsList.hide();
		ajaxLoader.fadeIn();
		//Send the POST to comments.php
		$.ajax({
    type: "POST", url: "comments.php", data: "action=update",
			 complete: function(data){
				  ajaxLoader.fadeOut();
				  commentsList.html(data.responseText);
				  commentsList.fadeIn(2000);
		 	}
		});
	}
	//Check if the form is filled in fully otherwise throw error
	function checkCommentsForm(){
   if(inputName.attr("value") && inputComment.attr("value"))
			  return true;
		  else
			  return false;
	}
 
	//Load the data in
	updateComments();
 
	//On submission of comment function
	$("#form").submit(function(){
   if(checkCommentsForm()){
			  var name    = inputName.attr("value");
			  var comment = inputComment.attr("value");
		  	//De-activate button whilst sending to avoid pressing twice
			  $("#submitComment").attr({ disabled:true, value:"Sending..." });
			  $("#submitComment").blur();
				 //send the post to comments.php
			  $.ajax({
				   type: "POST", url: "comments.php", data: "action=insert&name=" + name + "&comment=" + comment,
				   complete: function(data){
					  commentsList.html(data.responseText);
					  updateComments();
					  //Reactivate the send button
					  $("#submitComment").attr({ disabled:false, value:"submit comment" });
				  }
			 });
		}
		else alert("Please fill all fields!");
		//we prevent the refresh of the page after submitting the form
		return false;
	});
});
 

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS