Archive for the ‘JavaScript’ Category

How to delete DIV dynamically using JQuery

To download the code – Click Here

To delete or modify HTML contents we need to use JQuery or any other similar scripting methods.

In this example there are 3 DIV's and it can be deleted dynamically. When we click on the [ X ] button the corresponding DIV was deleted and also it send a POST request to delete.php so we can also delete the corresponding user from the database.

Live Demo


 

Read more »

How to find IP Address Location

To download source code – Click Here

In this example IP Address details are fetch from the website http://www.ip2location.com/. To view live demo click Show Location button in the below example

To download source code – Click Here

Read more »

How to eliminate cached data error from AJAX

Since AJAX load method uses jQuery get method internally, So that you may get cached data.

To remove this this error make sure to send a unique key as part of the query string and it will give you the new content /result /uncached data from server.

A simple ajax function

function unassign_shop(full_id)

{
var n=full_id.length;
var id=full_id.substr(9,n-9);
var commentContainer = $("div#unassigned_" + id).parent();
var str = 'remove_shop=removed&salesman_id=' + <?php echo $salesman_id; ?> + '&assign_id=' + id ;
commentContainer.slideUp('slow', function() {$("div#assigned_" + id).remove();});
 
document.getElementById("unassigned").innerHTML="<img src='../design/content/loading.gif' title='loading' />";
 
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("unassigned").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","ajax/fetch_unassigned.php?"+str,true);
xmlhttp.send();
}

If you use some functions like above  then you will get only the cached data because the query string is same as in most cases.

var str = 'remove_shop=removed&salesman_id=' + <?php echo $salesman_id; ?> + '&assign_id=' + id ;

One solution is to pass timestamp value as part of the query string so that it will generate a unique request.

Read more »