1

我需要制作一个类似 fmylife.com 中的温和系统。基本上我遇到的问题是使用 Ajax(不刷新页面)将 MySQL 查询加载到 div 中。MySQL 查询

$sql = mysql_query(“SELECT * FROM posts WHERE active =’0’” LIMIT 1) or die (mysql_error());
$row = mysql_fetch_array($sql);

HTML

<div class=”post-body”&gt;<?php echo $row[‘sitepost’];?></div>

按“是”或“否”按钮时应重新加载此数据。提前致谢。

4

3 回答 3

6

post.php中:

$sql = mysql_query("SELECT * FROM posts WHERE active ='0' LIMIT 1") or die (mysql_error());
$row = mysql_fetch_array($sql);
echo $row['sitepost'];

JS

<script type="text/javascript">
$(document).ready(function(){
   $("#yes").click(function(){
      $(".post-body").eq(0).load("post.php");
   })
})
</script>

HTML

<div id="yes">YES</div>
<div class="post-body"></div>
于 2012-06-17T10:30:47.007 回答
5

@mgraph 已关闭,但如果您想在按钮上执行此操作,请单击

post.php中:

//$UserOption will be Yes/No for button clicks or empty string for first load of the page

$UserOption = $_REQUEST['UserClicked'];

//Id will be set if a vote has been clicked or empty string if it's the first load
$Id = $_REQUEST['Id'];

//(Do something with $UserOption)

$sql = mysql_query("SELECT * FROM posts WHERE active ='0' LIMIT 1") or die (mysql_error());
$row = mysql_fetch_array($sql);
echo $row['sitepost'];

JS

<script type="text/javascript">
$(document).ready(function(){
   update(null,null);
})

function update(Id, Vote) {
    $(".post-body").eq(0).load("post.php?UserClicked=" + Vote + "&Id=" + Id);
}
</script>

HTML

<div class="post-body"></div>


<button type="button" onclick="update(1, 'Yes');">Yes</button>
<button type="button" onclick="update(1, 'No');">No</button>



<button type="button" onclick="update(2, 'Yes');">Yes</button>
<button type="button" onclick="update(2, 'No');">No</button>

备用 HTML/JS

<script type="text/javascript">
$(document).ready(function(){
    $("#yes").click(function(){
        update('Yes');
    })
    $("#no").click(function(){
        update('No');
    })
    update();
})

function update(Vote) {
    $(".post-body").eq(0).load("post.php?UserClicked=" + Vote);
}
</script>

<div class="post-body"></div>
<button id="yes" type="button">Yes</button>
<button id="no" type="button">No</button>
于 2012-06-17T10:36:24.837 回答
4

您可以添加$("html, body").animate({ scrollTop: 0 }, 500);. 因此,当您单击“是”按钮时,滚动向上移动到顶部。看起来很好。

于 2012-06-18T10:52:29.340 回答