我有一个while循环,在这个循环中,我有另一个while循环,我只想在button.click之后运行
我的profile.php中的javascript代码:
$('.commentbtn').click(function()
{
$post = $(this);
$.ajax({
url: 'includes/functions.inc.php',
type: 'GET',
data: {
'offset': 0,
'limit': 3
},
success: function(data)
{}
});
我在functions.inc.php中的php代码:
function getProfilePostModals($conn)
{
$uid = $_SESSION['userId'];
$sql = "SELECT * FROM posts WHERE uid=$uid AND type=1";
$query = mysqli_query($conn, $sql);
while ($postrow = $query->fetch_assoc())
{
$pid = $postrow['pid'];
//// some code for the posts /////
echo "<input id='opencomments".$pid."' class='commentbtn' type='button' value='Comment'>";
if (isset($_GET['offset']) && isset($_GET['limit'])) {
$commentssql = "SELECT * FROM postcomments WHERE pid=$pid";
$commentsquery = mysqli_query($conn, $commentssql);
while ($commentrow = $commentsquery->fetch_assoc())
{
//// some code for the comments that i want to run after "offset" and "limit" isset
}
}
}
}
在我开始添加if(isset)
之前,一切正常。但是现在不再有评论了。我想做的是为注释添加滚动功能,我知道它尚不完整的代码,我只是尝试看看是否可以让isset首先工作。预先谢谢你,我还在学习。
UPDATE:functions.inc.php里面有多个函数,在我的profile.php中,我有这段代码可以将所有内容传递给:
<?php include 'includes/functions.inc.php'; ?>
<?php getProfilePostModals($conn);?>
我不知道此信息是否有帮助。
这是我的评论和建议,可以帮助您解决遇到的问题。
我不知道您要在这里做什么,但是
$(this)
指的是按钮本身,它看起来好像不是您所需要的。我也看不到您实际上也在使用该变量。
$('.commentbtn').click(function()
{
$post = $(this);
您需要对收到的回复进行处理。在一切正常之前,我将建议以下内容。然后,您可以仅检查浏览器控制台中是否有从服务器发送的任何输出。
success: function(data)
{console.log(data);}
您输入的变量名称错误,
$row
应该为$postrow
。这将导致错误,因为['pid']
索引不存在。
while ($postrow = $query->fetch_assoc())
{
$pid = $row['pid'];
为此,如果未设置变量,则需要做一些事情,例如发送回响应,否则您将永远不知道出了什么问题。另外,如评论中所述,从不访问用户提供的数据,而无需先对其进行验证/消毒,并使用准备好的语句进行查询。
if (isset($_GET['offset']) && isset($_GET['limit'])) {
$commentssql = "SELECT * FROM postcomments WHERE pid=$pid";
$commentsquery = mysqli_query($conn, $commentssql);
while ($commentrow = $commentsquery->fetch_assoc())
{
//// some code for the comments that i want to run after "offset" and "limit" isset
}
} else {
echo "Expected $_GET values not received";
}
最后,由于没有错误处理,因此您无法知道查询是否失败。这是您需要在代码中实现的东西。