这是我的ajax帖子
<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>
现在我需要确定单击按钮的whitch类(在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>
单击该按钮时,将调用ajax函数,但该函数将显示在第一个状态div中,而不是在单击该按钮的div / class中。
答案 0 :(得分:0)
使用jQuery查找已单击的按钮。为您的按钮添加名称属性。
$().function({
$("some_button").click() {
clicked_name = $(this).attr("name");
}
});
答案 1 :(得分:0)
您可以使用某些相关数据更新隐藏字段,并在帖子中进行检查..
答案 2 :(得分:0)
首先,您需要意识到标签的“id”属性应该是唯一的。因此,实现此目的的正确方法是区分例如为其添加唯一编号的例程的ID。有点像这样:
$i = 0;
while ($row1 = mysql_fetch_array($event1))
{
$i++;
...
echo " ... <input type='submit' name='lol' value='dec$i' />
<div id='status$i'>...</div>";
}
然后你可以检查命令后得到的号码(我知道这是粗略的)
答案 3 :(得分:0)
而不是
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>";
使用
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>";
并且在javascript中,您可以轻松地通过使用整个对象在获得后点击哪一个,您可以使用数据属性或使用索引