0

So I have a table that shows online players in my gameserver and I'm trying to create a button that slaps that player, the script uses the players ID to slap him however my jquery script will only slap the first player in the table.

Any help is very much appreciated, here is my code

function slap(){
  
    var playerid = $("#playerid").val();
  
    $.post("q3/slap.php", { playerid: playerid }, function ( data ) {
       // populate data here
    });
}
   echo "<form><input type='hidden' id='playerid' value='$playerid'><input type=button class='actionbutton' value='Slap' onClick='slap()'></form>";

3
  • ID's need to be unique. You should probably switch to a class and attach an event handler to that directly in javascript / jQuery (as opposed to the onClick() handler). Commented Mar 3, 2016 at 15:31
  • whats your table like? and try using class instead of id. Commented Mar 3, 2016 at 15:32
  • What sets the value of $playerid? Commented Mar 3, 2016 at 15:39

2 Answers 2

1

First id is unique in HTML so if you have more than one player the id is useless.

But try:

function slap(playerid){
        
    $.post("q3/slap.php", { playerid: playerid }, function ( data ) {
       // populate data here
    });
}
   echo "<form><input type=button class='actionbutton' value='Slap' onClick='slap($playerid)'></form>";

that should work.

Sign up to request clarification or add additional context in comments.

1 Comment

What would I do if I wanted to use $playername as the variable instead? it doesn't seem to work when I change it to $playername
0

Id is used for unique identity. Use class instead of id

 echo "<form><input type='hidden' class='playerid' value='$playerid'><input type=button class='actionbutton' value='Slap' onClick='slap($playerid)'></form>";

and in your jquery

 function slap(playerid){

        $.post("q3/slap.php", { playerid: playerid }, function ( data ) {
           // populate data here
        });
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.