doufang3001 2014-11-28 09:31
浏览 168
已采纳

AJAX页面重新加载

I am creating a forum in which the posted user has option to delete his own posts. So, I am using ajax to send get request to delete a particular post and I am echoing null in place of the deleted post. The problem here is the post is getting deleted but the page is reloading after deletion which shouldn't happen as I am using ajax request. Can anyone help?


PHP CODE TO DISPLAY THE POSTS(ONLY A PART)

$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";

    }

Ignore if any parenthesis are not matching as this is only a part of the code.


javascript function which is called after delete link is clicked

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 page

$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条回答 默认 最新

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥15 如何关闭soui dummy wdn 进程
      • ¥20 含有jar文件的文件夹用tar -zcvf 命令压缩后要怎么还原回去?
      • ¥15 天宝TBC软件出现报错怎么解决
      • ¥15 micropython 从1开始切片报错
      • ¥15 请问该怎么做才能将文字说明和二维码一起作为png图片一起下载
      • ¥15 r语言 混频数据回归 midas_r
      • ¥15 人脸识recogizer.predict返回值id报错,KeyError,详情如下
      • ¥15 geowebcache部署失败
      • ¥20 关于adaboost+haar特征实现人脸检测过程的一些细节问题
      • ¥15 csv文件增加一列 为csv文件的名字