Calling PHP script with ajax and jQuery
Right now I have this:
echo "<a href='misc/removeWallComment.php?id=" .
$displayWall['id'] . "&uID" . $displayWall['uID'] . "&BuID" .
$displayWall['BuID'] . "' title='ta bort inlägg'>
<span class='removeWallComment'></span> </a>";
Its an icon with a link that removes the comment when you click.
Now, it goes to misc/removeWallComment.php
and echo out "comment removed". But I would like to integrate it with my current site, so you don't go to another page to delete the commehnt. With this I thought of using an ajax call to removeWallComment.php
.
Now as you see on the link it requir开发者_如何学运维es three variables, id
, uID
and BuID
, and I want to send it POST
, not GET
so the user can't see the variables in address bar. On success it should just alert ok.
How can I do this?
Just so that this question will have an answer:
var links = $('.removeWallComment').parent();
links.click(function(event) {
// Parse out the ids
var data = this.href.substring('?').split('&');
var id = data[0].substring(data[0].indexOf('=') + 1);
var uid = data[1].substring(data[1].indexOf('=') + 1);
var BuID = data[2].substring(data[2].indexOf('=') + 1);
$.post('misc/removeWallComment.php', {
'id': id,
'uid': uid,
'BuID': BuID
}, function(data){
// Success!
alert('OK');
});
event.preventDefault();
});
It doesn't matter if you use GET
or POST
, if you're using ajax you're user will never see the three id
s in the address bar. The three id
s, however, must be gotten somehow, so in this case we are parsing the href
value for them (the three id
must be stored somewhere, preferably on the element itself for easy retrieval. If you want to hide the id
s for security reasons this isn't the best way to do it).
精彩评论