简体   繁体   English

PHP中的Ajax找到正确的div / class

[英]Ajax in php finding right div/class

<div id = 'eventcontainer' >
<span id = 'class'>
<?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>";

}
$('.eventcontainer.button').click(function() { 
$.post('javas.php', function(data) {
   $(this).parent('div').find('.status').html(data);
})
});
?>
</div>
</span>

I get an error when trying to run this code. 尝试运行此代码时出现错误。 What i am trying to do is try to read which div/class the button is being clicked in, then perform the relative ajax/jquery functionality in that class only. 我想做的是尝试阅读单击该按钮的div / class,然后仅在该类中执行相对的ajax / jquery功能。

This is the Ajax/Jquery function ajax_post(){ 这是Ajax / Jquery函数ajax_post(){

  $('.eventcontainer.button').click(function() { 
$.post('javas.php', function(data) {
   $(this).parent('div').find('.status').html(data);
})
});


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

}

As the comments have noted, you should not have jQuery in your PHP tags. 正如评论所指出的那样,PHP标记中不应包含jQuery。 Add some script script tags to your page's head, add a document.ready handler, then put the script in there. 在页面的头部添加一些脚本脚本标签,添加document.ready处理程序,然后将脚本放入其中。

Just note that once you accomplish this, you'll still have some problems since you're getting your this values mixed up. 只需注意,一旦完成此操作,由于将this值混淆了,您仍然会遇到一些问题。

$('.eventcontainer.button').click(function() { 
   $.post('javas.php', function(data) {
      $(this).parent('div').find('.status').html(data); 
   })
});

Once you hit the callback of your $.post call, this no longer is equal to the item you clicked on. 按下$.post调用的回调后, this将不再等于您单击的项目。 You'll need to save that element in a local variable before the call to post. 在调用发布之前,您需要将该元素保存在局部变量中。

Something like 就像是

$('.eventcontainer.button').click(function() { 
    var self = this;
    $.post('javas.php', function(data) {
      $(self).parent('div').find('.status').html(data);
   })
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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