刷新使用Ajax一个DIV中的MySQL数据 [英] Reload MySQL data inside a DIV using Ajax

查看:182
本文介绍了刷新使用Ajax一个DIV中的MySQL数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要做一个温和的系统像在fmylife.com。基本上,这个问题我已经被加载使用Ajax(无页面清爽)到一个div的MySQL查询。 MySQL查询

I need to make a moderate system like the one in fmylife.com. Basically the problem I have is loading the MySQL query using Ajax (without page refreshing) in to a div. MySQL query

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

HTML

HTML

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

此数据应重新装入时,pressing是或否按钮。提前致谢。

this data should be reloaded when pressing "yes" or "no" buttons. thanks in advance.

推荐答案

@mgraph接近,但如果你想这样做的一个按钮,点击

@mgraph is close but if you want to do this on a button click

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>

这篇关于刷新使用Ajax一个DIV中的MySQL数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆