Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

JQuery function calling itself to reload issue

Posted on 2010-11-22
3
Medium Priority
?
783 Views
Last Modified: 2012-05-10
Hello,

I have the below JQuery setup that I'm trying to use.  The loadGallery function gets called when the page loads and loads some images with a link to delete the image (only link is in the code below).  When I click the deleteImage link, the deleteImage function gets call and the post to deleteImage.php delete's the image from the database, but then I need to reload the images on the page, so I call the loadGallery function, but the post to images.php never gets executed as I've put alerts before and after it and only the alert prior shows.  Any ideas?
$(document).ready(function(){
function loadGallery(albumId, albumTitle){	
$.post("images.php",{albumId:albumId},function(theXML){
	$('image',theXML).each(function(i){    
		var deleteLinkText = $(document.createTextNode("Delete"));
		var deleteLink= $(document.createElement("a"))
			.attr("class", "adminDelete")
			.attr("href", "")
			.attr("alt", id)
			.append(deleteLinkText )
			.click(function(){
				deleteImage(id, "image", "Delete Image Permanently?", albumId, albumTitle);	
				loadGallery(albumId, albumTitle);				
			});
		}		
	}
} 
function deleteImage(imageId, type, msg, albumId, albumTitle) {
	if (confirm(msg)){ 
		$.post("deleteImage.php", {imageId:imageId});			
	}
}    
}

Open in new window

0
Comment
Question by:cgray1223
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 3

Expert Comment

by:boraganesh
ID: 34186290
once you call post function...means your going to another page....its not going to excute any other things as control goes to another page.

.click(function(){
                        deleteImage(id, "image", "Delete Image Permanently?", albumId, albumTitle);      
                        loadGallery(albumId, albumTitle);                        
                  });


if (confirm(msg)){
           $.post("deleteImage.php", {imageId:imageId});                  
      }

its never calling loadGallery as function deleteImage taking cotrol to another page deleteImage.php.

you can do one thing..from deleteImage.php once you done with your work..deleting images you can call the previous page agan.

or

you can use the ajax to call the deleteImage.php thats the only solution am seeing



0
 
LVL 16

Accepted Solution

by:
Justin Mathews earned 2000 total points
ID: 34190621
Add a success handler to your $.post("deleteImage.php") and call loadGallery() from that as:


function deleteImage(imageId, type, msg, albumId, albumTitle) {
 if (confirm(msg)){ 
  $.post("deleteImage.php", {imageId:imageId}, function(){loadGallery(albumId, albumTitle);});			
 }
}

Open in new window

0
 

Author Closing Comment

by:cgray1223
ID: 34193695
thanks for the help, great idea!
0

Featured Post

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

jQuery is a JavaScript library that greatly simplifies JavaScript programming. AJAX is an acronym formed from "Asynchronous JavaScript and XML."  AJAX refers to any communication between client and server, when the human client does not observe a…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question