简体   繁体   中英

How can i select the right div

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>";


}

?>

I need to determine in which div the button was clicked in, this is the ajax function, i need to echo num in the php tags however i only want it to be echoed in the class/post where the button was clicked as it is in a while loop and there are many replicas of the button.

<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...";

}

Then how can I display the information/data in that specific Div in which the button was clicked. As i have it now the echoing takes place in the first class it picks up! Much appreciated, thanks!

If I understood correctly you have multiple buttons that can be pressed and you want to know witch was pressed.

Add this to the onclick event

   function(){ whateverFunctionYouUsed(this)}

Or you can just get the attribute you need before sending it to your function.

If you have firefox+firebug use this to see all properties of the object and take what you need:

  function(){ 
          console.log(this)
          whateverFunctionYouUsed(this)
  }

With jQuery it's quite easy:

$("yourContainerId > div").click(function () {
  var index = $("div").index(this);
  alert(index);
});

http://api.jquery.com/index/

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