Select the identifier of the submitted form to put in jquery variable

advertisements

I'm making a jokes website. Each joke is stored in a record in mysql db. When the jokes are put together on a page, there is a vote button with the class=votebutton. When someone clicks this, I'd like the id of the form and the user's IP address sent via jquery's ajax function to my vote processing .php file. I have 2 questions: 1. Is it right to use $(".votebutton").click() as the selector to run this function? 2. How do I select the id of the form being submitted to construct the id variable in the jquery function?

 $(function() {
      $(".votebutton").click(function() {
      var id = //I'd like this to be the id of the form submitted
      var ip = //I'd like this to be the ip of the user
      var dataString = 'id='+ id = '&ip=' + ip;
      //alert (dataString);return false;
      $.ajax({
        type: "POST",
        url: "bin/vote.php",
        data: dataString,
        success: function() {
          //something to do on success
        }
      });
      return false;
   });
 });

Also, one more question if you guys have a minute - what is a good way to get the IP of the visitor. I expect there to be about 10 of these forms per page (one for each joke)...

Thanks very much for the help...


Is it right to use $(".votebutton").click() as the selector

Yes

How do I select the id of the form being submitted

Common practice is to store the ID as part of the element ID, for example:

<button id="vote_54321" value="Vote!" class="votebutton" />

Than, in your function you can get the ID:

var id = $(this).attr('id').split('_')[1];

There is no way to get the client IP from javascript, that you should get it in your serverside php code:

$userip = $_SERVER['REMOTE_ADDR'];