views:

141

answers:

2

Hello,

I have a page where I am displaying a list of users and next to each user - there is "Add as Friend" link.

Now when a user clicks this "Add as Friend" link - I want to call Jquery and submit that request to the backend PHP.

Typically my experience with Jquery involves having a single form in the page and submitting that form via Jquery - Each form has an ID - Using that ID I call the submit function $("#invite_form").submit(function() - I access the form elements this way var emailval = $("#emails").val();

But now here I dont have a form and this friends list is being generated in a loop. So here are the doubts that I have

1) Do I need to create a unique id in the loop for each a href tag

2) How do I change this $("#invite_form").submit(function() - Will it become ("#ahref1").click(function() where ahref1 is the unique id of the href tag

3) How do I access the friend_id field in Jquery function which is present in the href value something like href="/action?friend_id=32"

Not sure if I'm going on the right track Thanks

+1  A: 

You can submit to a back-end script using $.post() or $.get() in jQuery

An example:

$("#ahref1").click(function(){
  var this_friend_id = $(this).prev('input.friend_id').val();
  // Assuming that you store the id in a hidden input field like <input type="hidden" class="friend_id" val="32" /> which is defined just before the a href.
  $.get('/action', {
     friend_id: this_friend_id
    }, function(data){
       // callback after the ajax request
       alert("The response is : "+data);
    });
});

This should solve the problem.

GeekTantra
Thanks GeekTantra - but how do I handle the different ahref id'sLike I said - these are being generated in the loop - so there will be 10 different rows. Now even if I create a unique id for each ahref tag - I will need to create 10 different functions like $("#ahref1").click(function() $("#ahref2").click(function() $("#ahref3").click(function() $("#ahref4").click(function()Similar issue I will have with the input field with friend idThanks
Gublooo
I'll add a comment rather than my own answer, since GeekTantra almost has it perfect. The magic of jQuery is that a selector can match multiple elements, so a single click function can serve for all of your links. Note that the ID doesn't appear inside GeekTantra's function -- it just uses "this" to know which link was clicked. So if you change "$("#ahref1")" at the beginning of that code to "$("id^='ahref'")" the click function will apply to any element whose id starts with "ahref", and it should work for all of your links.
JacobM
Thankyou for sharing that information
Gublooo
A: 

1) No, you don't. You can always create a jquery loop, with .each(), and customize your script like

$(document).ready({
    $('#friend_list a').each(function () {
        // Point to current <a>
        var $this = $(this);
        // ... your code for each a in your list
    });
});

2) You can change the submit function like

$(document).ready({
    $('#friend_list a').each(function () {
        var $this = $(this);
        // Save current href
        var href = $this.attr('href');
        // When user click
        $this.click(function(e) {
            // Doesn't follow the href path
            e.preventDefault();
            // ... your custom operation here
        });
    });
});

3) You can use regex

$(document).ready({
    $('#friend_list a').each(function () {
        var $this = $(this);
        var href = $this.attr('href');
        $this.click(function(e) {
            e.preventDefault();
            // Assuming your link is in form "/action?friend_id=32"
            var re = new RegExp("\d+");
            var id = re.exec(href);
            // I prefer AJAX method, this is just a sample, to complete
            $.post('/action', 
                   { friend_id: id }, 
                   function(data) {
                      // ... result here
                   });
        });
    });
});
Sig. Tolleranza
Your code example worked perfectly for my needsThankyou so much for taking the time to put that up Appreciate it
Gublooo
You're welcome.
Sig. Tolleranza