SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Zealot silverviper's Avatar
    Join Date
    May 2005
    Location
    Norway
    Posts
    125
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Ajax call php script

    Hi

    Im making a price calculating tool where i need to
    have a php script run in the "background" no refresh.

    I have a dynamic page with Add links that call up the list.php and pass two variables

    Code:
    <a href="list.php?action=add&id=23">Add</a>
    <a href="list.php?action=add&id=13">Add</a>
    <a href="list.php?action=add&id=3">Add</a>
    <a href="list.php?action=add&id=65">Add</a>
    etc...
    Been googling and this is the closest I've gotten, it works, but
    only with a form button, and i can't get it to get the id from the button

    Code:
    <script type="text/javascript">
    $(document).ready(function() {
    $('#AddMe').on('click', function () {
    $.ajax({ url: 'cart.php?id=6',
             data: {action: 'add'},
             type: 'get',
             success: function(output) {
                          //alert(output);
                      }
    });
        });
    });</script>
    does anyone have an idea how to solve this, and preferably using "a href" style button/link?

    thx

    br

    Silverviper
    He who knows that enough is enough will always have enough

  2. #2
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,441
    Mentioned
    45 Post(s)
    Tagged
    13 Thread(s)
    Hi silverviper,

    If I understood correctly, this should do what you want:
    HTML Code:
    <a class="calcAdd" href="list.php?action=add&id=23">Add</a>
    <a class="calcAdd" href="list.php?action=add&id=13">Add</a>
    <a class="calcAdd" href="list.php?action=add&id=3">Add</a>
    <a class="calcAdd" href="list.php?action=add&id=65">Add</a>
    
    <script type="text/javascript">
        $('.calcAdd').on('click', function(e){
            $.get(this.href, function(data){
                console.log(data);
            })
        });
    </script>

  3. #3
    SitePoint Zealot silverviper's Avatar
    Join Date
    May 2005
    Location
    Norway
    Posts
    125
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi

    Thx for the reply,

    its not quite what i needed,
    I need the list.php to be call as a background task so that the "main.php"
    doesn't have to reload.

    the list.php is sort of a shopping cart embedded in the top section of main.php

    Code:
    <script type="text/javascript">
    $(document).ready(function() {
       $('#list').load('list.php', function() {
       });
    });
    </script>
    <div id="list"></div>
    Silverviper
    He who knows that enough is enough will always have enough

  4. #4
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,441
    Mentioned
    45 Post(s)
    Tagged
    13 Thread(s)
    Ah, sorry, I misunderstood.. so there would only be one link like this on the page:

    HTML Code:
    <a href="list.php?action=add&id=23">Add</a>
    and clicking the link will load the contents of list.php into <div id="list"></div> on the main page, is that correct?

  5. #5
    SitePoint Zealot silverviper's Avatar
    Join Date
    May 2005
    Location
    Norway
    Posts
    125
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi

    sorry for the confusion .)

    the <div id="list"></div> on the main.php will display all added items

    main.php is dynamic and the number of links will vary

    have a look here to get the idea http://silverviper.org/calc/calcs.php

    Chris
    He who knows that enough is enough will always have enough

  6. #6
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,441
    Mentioned
    45 Post(s)
    Tagged
    13 Thread(s)
    OK, so this should do it:

    Code JavaScript:
    $('.calcAdd').on('click', function(e) {
        e.preventDefault();
        $('#list').load(this.href);
    });

  7. #7
    SitePoint Zealot silverviper's Avatar
    Join Date
    May 2005
    Location
    Norway
    Posts
    125
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    $('.calcAdd').on('click', function(e) {
        e.preventDefault();
        $('#list').load(this.href);
    });
    Code:
    <a class=\"calcAdd\" href=\"cart.php?action=add&id=".$id."\">Add</a>
    this still redirects to the cart/list script

    Chris
    He who knows that enough is enough will always have enough

  8. #8
    SitePoint Zealot silverviper's Avatar
    Join Date
    May 2005
    Location
    Norway
    Posts
    125
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    this solved my problem

    Code:
    <script type="text/javascript">
    $(document).ready(function() {
    $('.calcAdd').on('click', function(e) {
        e.preventDefault();
        $('#cart').load(this.href);
    	   });
    });
    </script>
    thx fretburner
    He who knows that enough is enough will always have enough


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
  •