/    Sign up×
Community /Pin to ProfileBookmark

Understanding this Delete Function

Fairly new to Javascript, inherited a web site, trying to decipher how this function works. By the way, If I am not including code the proper way, please tell me how I should be doing it, happy to learn the right way.

function del(item_id) {
if(confirm(‘Are you sure you want to delete this item?’)) {
$.post(“<?=$_SERVER[‘PHP_SELF’]?>”, {
id:item_id,
action:’delete’,
callback: function(data, textStatus) {
$(“#faqs_
“+item_id).remove();
}
});
}
}

This function confirms/cancels the Delete action, then refreshes the screen to update the list of records. It does not do the actual deletion; that is in HTML and PHP.

The function works in one location on my site, and was copied to another. While the actual deletion of the data record works fine on the new page, the refresh of the screen is not working.

Obviously, the $_SERVER[‘PHP_SELF’] portion of the code is never executed. Trying to learn why.

There are two variables in the function: item_id and “#faqs_”. I can confirm item_id is a legit value; it is not the problem.

I do not know what “#faqs_” is all about. Clearly, it is related to the actual page (the function came from a page all about FAQS about the company), but I can find NO variables, table, field, anything else that uses this same name. It seems to be just a marker of some sort.

So, I am not understanding what the callback: function (…) is trying to do, what data it needs to do it, and what I should replace “#faqs_” with.

Any ideas how this function works, what I might try in place of #faqs_?


****

Just for clarification:

This function is called from an HTML form as:

<div class=”delete”><input type=”button” class=”button” value=”Delete” onclick=”del(<?=$vers_obj->id?>)” /></div>

This code works; ‘$vers_obj->id’ is a valid record number.

The actual Delete function is a PHP function and deletes the record appropriately. The delete() function is a commonly used PHP function used successfully throughout the site.


*****

Thanks very much appreciated,

tc

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@rnd_meNov 12.2010 — #faqs_1 points to an html element whose id is = "faqs_1"
Copy linkTweet thisAlerts:
@WTCauthorNov 12.2010 — Son of a gun, there it is...<li id=faqs_...how the heck did I miss that. Strangely, adjusting this ID and the javascript function to be in synch worked on one of my pages, but not another. Will keep looking to see what else may be wrong...

Thanks rnd_me

tc
Copy linkTweet thisAlerts:
@WTCauthorNov 12.2010 — Nevermind! Found the bug, my typo...it's all good!
×

Success!

Help @WTC spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...