SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Apr 2013
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to call php page with jquery ajax?

    I have a anchor tag with href like this:
    HTML Code:
    <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.

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,716
    Mentioned
    103 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by vny.009 View Post
    I have a anchor tag with href like this:
    HTML Code:
    <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:

    Code javascript:
    <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:

    Code javascript:
    $('#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:

    Code php:
    <?php
    echo 'Success';
    ?>
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Member
    Join Date
    Apr 2013
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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
    HTML Code:
    <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.

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,716
    Mentioned
    103 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by vny.009 View Post
    So, does load() useful in this case too?
    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:

    This is a shorthand Ajax function, which is equivalent to:
    Code javascript:
    $.ajax({
      url: url,
      data: data,
      success: success,
      dataType: dataType
    });

    So instead of you doing the following ajax request:

    Code javascript:
    $.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()

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

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

    Code javascript:
    $(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.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #5
    SitePoint Member
    Join Date
    Apr 2013
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •