【发布时间】:2018-07-09 17:32:46
【问题描述】:
直到我发现它实际上是一个问题才开始出现这个问题。我为这个令人困惑的问题道歉。基本上,我正在用 php 收集一个 mysql id,并使用 ajax 的 POST 方法尝试将该 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>
这里是访问modal的锚标签,负责保存id:
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 帖子中显示我的查询结果并将其放在页面上?
笔:codepen.io/anon/pen/djbgKj
【问题讨论】:
-
第一次运行你的 php,它将导致空的 var 转储,因为没有传递 POST 参数。然后,当 ajax 启动时,它会传递 POST 参数......嗯............不知何故,这个问题看起来非常熟悉。
-
没错。既然你这么说了。我认为 php 只是在您进入页面后立即运行?但话又说回来,我在 post 方法显示在模态之前激活它。
-
cooking.php是否与使用 ajax javascript 块生成原始页面的 php 相同?当我认为您只想要跨度列表时,似乎会通过该 ajax 调用返回大量 html。 -
@IncredibleHat 我不确定你的意思。但是大部分 HTML 是从 php 查询中自动生成的。
标签: javascript php jquery ajax post