0

enter image description here
I have a table with a button in each line. Each line has its own specific id. Is there a function in PHP that allows me to specify exactly which button is pressed? I mean when I click first button it should notify me that it was a button within a line with id='1', etc.

@edit

function usunKontakt() {
    var temp = $(this).parent().parent().children(":first-child").text();
    console.log("Javascript: "+temp);
}
$("button.usun").click(usunKontakt);

That's exactly what I want to do in PHP, what I made in jQuery. Is this possible in PHP?

Humberd
  • 2,070
  • 2
  • 15
  • 29

3 Answers3

0

OK you have a couple problems with your concept.

First PHP is a server side language, which means that it's functions occur a long before the button is actually pressed.

Second the 'id' attribute is used as the Elements identifier, so it's should be unique and should NOT be a number or either start with a number. I strongly recommend you to read this post: naming convetions for html's ids and this: valid id attribute

Now, when you say that 'me' at:

when I click first button it should notify me

You refer me => as User

OR

me => as Server?

If you edit your question to be more specific, it would be easier to help you)

Community
  • 1
  • 1
Nikita Kurtin
  • 4,811
  • 3
  • 41
  • 44
  • I know that naming was a bit inappropriate. I know how to do it in Javascript. What i meant was for example when i Click button it should print on a page its id. When someone mentioned here in Javascript I can use parent method and then find its attribute id, but is there something similar in PHP? – Humberd Sep 24 '15 at 20:27
  • Like I said, PHP is a server side language, so *NO, you don't handle click with PHP*. For that you should use Javascript and there are many different ways to do that. If you need to notify your server after the click, you can just send a request, for example HTTP POST request. – Nikita Kurtin Oct 08 '15 at 13:44
0

Would something like this work?

$('button').click(function() {
  console.log($(this).parent().siblings().filter(':first').html());

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tr>
    <td>1</td>
    <td>
      <button>test</button>
    </td>
  </tr>
  <tr>
    <td>2</td>
    <td>
      <button>test</button>
    </td>
  </tr>
  <tr>
    <td>3</td>
    <td>
      <button>test</button>
    </td>
  </tr>
</table>

Depends on your table structure, but you could search for the first TD of each TR and catch its html contents.

George Irimiciuc
  • 4,147
  • 3
  • 36
  • 75
0

Try this jquery inside a button function

$(this).parents(".parent-id/class").find(".content id/class ").show();

// PHP

// use different names for each buttons

if(isset($_POST['buttonname']))
{

function;
}
Hisham
  • 99
  • 14