How to call php page with jquery ajax?

I have a anchor tag with href like this:

<a href="page.php?accept=id">Link</a>

.

Id is coming after a database query.
I want to make it with ajax. How to call php page with ajax?
On success, that link should be disappeared and a success text should be appear.

I am confused calling ajax with parameters. Any quick help will be appreciated.

With jQuery it’s quite easy. If the link is with others in some sort of contained section:


<div id="links">
    <a href="page.php?accept=id">Link</a>
    <a href="page.php?accept=id">Link</a>
</div>

Then you can use something like the following:


$('#links a').on('click', function (evt) {
    evt.preventDefault();
    
    var link = evt.target,
    span = $('<span>... processing ...</span>');
    $(link).replaceWith(span);
    $(span).load(link.href);
});

The outputted content from page.php will then be placed in the span. That output could be as simple as just:


<?php
echo 'Success';
?>

As i know jquery load() function is only for replacing content from a another file’s content. But this is a add friend link with parameter “accept”
like this

<a href="page.php?accept=<?php echo $row['id']?>">Aceept</a>

Now, without ajax, when i click on this. The accept param is set and accept.friend.php script runs and it runs only when accept param is set.

So, does load() useful in this case too?

By the way thanks to you for your quick reply.

Yes. jQuery’s .load() method loads the same page.php file with the querystring parameters that you have there, and places the response from the server as HTML code in the element that .load() is performed on.

The .load() documentation page says:

This method is the simplest way to fetch data from the server. It is roughly equivalent to $.get(url, data, success) except that it is a method rather than global function and it has an implicit callback function. When a successful response is detected (i.e. when textStatus is “success” or “notmodified”), .load() sets the HTML contents of the matched element to the returned data.

So .load() is a shortcut for jQuery’s .get() method.

The .get() documentation page says:

[indent]This is a shorthand Ajax function, which is equivalent to:


$.ajax({
  url: url,
  data: data,
  success: success,
  dataType: dataType
});

[/indent]

So instead of you doing the following ajax request:


$.ajax({
  url: link.baseURI + link.pathname,
  data: link.search,
  success: function (response) {
    $(span).html(data);
  },
  dataType: 'html'
});

you can instead achieve the same thing by using .get()


$.get(link.href, function (response) {
  $(span).html(data);
});

which you can also achieve in an easier way by using .load()


$(span).load(link.href);

It’s all ajax. It’s just that .load() helps to remove some of the unwanted complexity from what you are doing, so that you can more easily focus on what the code does, instead of on how to achieve your intended end result.

Hmm. Thanks a lot for this detailed info. You can also accept my friend request. It will help me to learn from a great expert in Js.