AJAX页面重新加载

时间:2014-11-28 09:31:28

标签: javascript php ajax

我正在创建一个论坛,其中发布的用户可以选择删除自己的帖子。所以,我使用ajax发送get请求删除一个特定的帖子,我回复null代替删除的帖子。这里的问题是帖子被删除但是页面在删除后重新加载,这不应该发生,因为我正在使用ajax请求。有人可以帮忙吗?


PHP代码显示帖子(仅限部分)

$sql = "SELECT * FROM classposts where branch='$branch' and joindate='$year' and class='$class'    order by".$orderby." desc";
$result = $connection->query($sql);
if(isset($_SESSION['username'])){
$username=$_SESSION['username'];
}

while($row = $result->fetch_assoc()) {
    echo "<span id='postspan".$row['id']."' name='postspan".$row['id']."' >";
    echo "<span id='editspan".$row['id']."' name='editspan".$row['id']."' >";
    echo "-----------------------------------</br>";
    echo "-----------------------------------</br>";

    echo "Posted By: ".$row['user']."&nbsp&nbsp&nbsp&nbsp";
    if($username==$row['user']){
        echo "<a href='classwall.php' onclick='deletepost(".$row['id'].")' >DELETE POST</a>&nbsp&nbsp&nbsp";

    }

如果任何括号不匹配则忽略,因为这只是代码的一部分。


点击删除链接后调用的javascript函数

function deletepost(postid){
    var r=confirm("Are you sure you want to delete the post?");
    if(r==true){
    var xmlhttp=new XMLHttpRequest();
    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {

                document.getElementById("postspan"+postid).innerHTML=xmlhttp.responseText;

        }
    };
    xmlhttp.open("GET","deletepost.php?pid="+postid,true);
    xmlhttp.send();
    }else{}
}

deletepost.php页面

$pid=$_GET['pid'];
$query="delete from dubiousposts where id='$pid'";
$sql=mysqli_query($connection,$query);
$query="delete from genuineposts where id='$pid'";
$sql=mysqli_query($connection,$query);

$query="delete from posts where id='$pid'";
$sql=mysqli_query($connection,$query);

if($result=mysqli_fetch_array($sql)){
echo "";
}

1 个答案:

答案 0 :(得分:0)

你已经给了call.php类页面的href链接,这就是你去其他页面的原因。

变化

echo "<a href='classwall.php' onclick='deletepost(".$row['id'].")' >DELETE POST</a>&nbsp&nbsp&nbsp";

echo "<a href='javascript:void(0);' onclick='deletepost(".$row['id'].")' >DELETE POST</a>&nbsp&nbsp&nbsp";