简体   繁体   中英

How to determine which div/class a button has been clicked in

This is my ajax post

<script language="JavaScript" type="text/javascript">
  var num = 1;
  function ajax_post(){
  // Create our XMLHttpRequest object
  var hr = new XMLHttpRequest();
  // Create some variables we need to send to our PHP file
  var url = "javas.php";
  hr.open("POST", url, true);
   // Set content type header information for sending url encoded variables in the request
  hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  // Access the onreadystatechange event for the XMLHttpRequest object
  hr.onreadystatechange = function() {
  if(hr.readyState == 4 && hr.status == 200) {
        var return_data = hr.responseText;
        document.getElementById("status").innerHTML = return_data;
    }
}
// Send the data to PHP now... and wait for response to update the status div
hr.send("num=" + (++num)); // Actually execute the request
document.getElementById("status").innerHTML = "processing...";

}
</script>

Now I need to determine whitch class the button was clicked in (in php)

<?php
   //Getting posts from DB
   $event1 = mysql_query("SELECT post,date,memid FROM postaction WHERE memid = '$id' ORDER  BY date DESC LIMIT 5;");
  while ($row1 = mysql_fetch_array($event1))
  {
       $event = $row1['post'];
       $timeposted = $row1['date'];
       $eventmemdata = mysql_query("SELECT id,firstname FROM users WHERE id = '$id' LIMIT 1");
        while($rowaa = mysql_fetch_array($eventmemdata))
        {
            $name = $rowaa['firstname'];
            $eventlist = "$event <br> $name";
    }
        echo " <div id = 'eventer'> $timeposted <br>$eventlist</div> <input name='myBtn'            type='submit' value='increment' onClick='javascript:ajax_post();'>
               <input name='lol' type='submit' value='dec' onClick='javascript:ajax_posta();'>
               <div id = 'status'>lol</div>";
        echo "<br>";
  }
?>
</span>
</div>

When the button is clicked, the ajax function is called, however the function is being displayed in the first status div rather than in the div/class the button was clicked in.

instead of

 echo " <div id = 'eventer'> $timeposted <br>$eventlist</div> <input name='myBtn'            type='submit' value='increment' onClick='javascript:ajax_post();'>
               <input name='lol' type='submit' value='dec' onClick='javascript:ajax_posta();'>
               <div id = 'status'>lol</div>";
        echo "<br>";

use

  echo " <div id = 'eventer'> $timeposted <br>$eventlist</div> <input name='myBtn'            type='submit' value='increment' onClick='javascript:ajax_post();'>
               <input name='lol' type='submit' value='dec' onClick='javascript:ajax_posta(this);'>
               <div id = 'status'>lol</div>";
        echo "<br>";

and in javascript you can easily get which one was clicked by using the whole object after receiving it you can use data- attributes or use the index

Use jQuery to find out which button has been clicked. Add a name attribute to your button.

    $().function({
      $("some_button").click() {
      clicked_name = $(this).attr("name");
      }
    });

您可以使用一些相关数据更新隐藏字段,并在发布后进行检查。

At first, you need to realize that "id" attribute of the tag is supposed to be unique. So the correct approach to implement this would be to differentiate the IDs for exemple appending a unique number to them. Kinda like this:

$i = 0;
while ($row1 = mysql_fetch_array($event1))
{
   $i++;

   ...

   echo " ... <input type='submit' name='lol' value='dec$i' />
       <div id='status$i'>...</div>";
}

You can then check for the number you get after the command (I know this is crude)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM