这个问题一直没有发生,直到我发现这实际上是一个问题。对于这个令人困惑的问题,我深表歉意。基本上,我使用 php 收集 mysql id,并使用带有 ajax 的 POST 方法来尝试将该帖子 id 放入查询中。问题是查询有效并且数据显示在控制台上,但不显示在屏幕上:
在屏幕本身上,除了 array(0){} 之外我什么也没看到。因为var_dump
。
看起来似乎它没有在屏幕上更新,但它在控制台上更新。这是我的模态代码的样子:
<div class='modal fade' id='small' role='dialog'>
<div class='modal-dialog'>
<div class='modal-content'>
<h3 class="fetchuserid" style='text-align: center;'>
<?php
$db = mysqli_connect("localhost", "root", "password", "testdb");
$uid = $_POST['postuserid'];
var_dump($_POST); <-- This is where you should be seeing the array(1){["postuserid"] => string(2) "10"} `10` being the id I clicked.
$ssql = "SELECT * FROM foodid WHERE postid=$uid";
$rresult = mysqli_query($db,$ssql);
while($lrow = mysqli_fetch_assoc($rresult)){
echo $lrow['countid'];
echo '<span class="postid_count">'.$lrow['countid'].'</span>'; <-- This is where you should be seeing all of those span in the console.
}
?>
</h3>
</div>
</div>
</div>
这是用于访问模式并负责保存 id 的 anchor 标记:
echo '<a href="#" data-postuserid="'.$row['id'].'" id="smallbtn" name="smallbtn" data-toggle="modal" data-target="#small" data-modal="small">';
最后这是我的 ajax:
$(document).on('click','#smallbtn',function(){
var postuserid = $(this).data('postuserid');
$post = $(this);
console.log($(this).data('postuserid'));
$.ajax({
url: 'cooking.php',
type: 'POST',
data: {
'postuserid' : postuserid
},
success:function(data)
{
console.log(data);
$post.parent().find('span.postid_count').text(data); <-- Here is what I tried to do to I guess "update" the data from the POST. No success.
return data; <-- I read other questions and was told to do this. No success.
},
error:function(data)
{
$( document ).ajaxError(function( event, request, settings )
{
$("#banner-message > p").text('Sorry, we are having a trouble in this url "'+settings.url+'!"')
});
}
});
});
注意:这一切都发生在一个页面中,没有刷新,因此不会丢失数据。我确实检查了我的网络选项卡,看看这是否是问题所在,并且使用 200 POST 方法一切正常。它还在网络和控制台中显示了正确的 postuserid。
我不确定我在这里问的问题是否正确,但我们非常欢迎任何反馈!基本上,如何从控制台显示 ajax post 的查询结果并将其放在页面上?
笔:codepen.io/anon/pen/djbgKj
最佳答案
这会对你有帮助
document.getElementById("some_id").innerHTML = data;
请尝试一下
根据您的情况,您可以这样做:
$post.parent().find('span.postid_count').innerHTML = data;
关于javascript - ajax POST数据结果在控制台,却显示在页面上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51251103/