编辑帖子AJAX,PHP,javascript

时间:2014-11-28 13:02:11

标签: javascript php jquery

我正在创建一个用户可以编辑帖子的论坛。我不知道该怎么做,但我尝试了以下内容:


显示帖子的PHP代码

while($row = $result->fetch_assoc()) {

echo "<div class='postclass'>";
echo "<span id='postspan".$row['id']."' name='postspan".$row['id']."' >";
echo "<span id='editspan".$row['id']."' name='editspan".$row['id']."' >";

echo "</br>";

echo "Posted By: &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp <span class='bold'> ".$row['user']."</span>";
if($username==$row['user']){
    echo "&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp";

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

    echo "<a href='javascript:void(0)' onclick='editpost(".$row['id'].",'".$row['subject']."','".$row['post']."')' >EDIT </a></br>";
    }else{
        echo "</br>";
}

点击编辑链接时调用的javascript函数

function editpost(postid,subject,post){

    var xmlhttp=new XMLHttpRequest();
    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {

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

        }
    };
    xmlhttp.open("GET","editpost.php?pid="+postid+"&subject="+subject+"&post="+post,true);
    xmlhttp.send();
}

editpost.php页面

$pid=$_GET['pid'];
$subject=$_GET['subject'];
$post=$_GET['post'];
}else{
    $pid="";
    $subject="";
    $post="";
}
echo "<form method='POST' action='globalwall.php'>
    <p>Subject:
        <label for='subject'></label>
        <input type='text' name='subject' id='subject' value='".$subject."' />
    </br>
    </p>
    <p>Post: <br />
        <label for='post'></label>
        <textarea name='post' id='post' cols='45' rows='5' value='".$post."'></textarea>
    </p>
    <p>
        <input type='submit' name='postbutton' id='postbutton' value='Edit' />
    </p>
</form>";

当我点击编辑按钮时,没有发生任何事情。即使我在javascript函数中发出警告消息,它也不会弹出。有人可以帮忙吗?


1 个答案:

答案 0 :(得分:0)

替换

echo "<a href='javascript:void(0)' onclick='editpost(".$row['id'].",".$row['subject'].",".$row['post'].")' >EDIT </a></br>";

对齐
echo "<a href='javascript:void(0)' onclick='editpost(".$row['id'].",\"".$row['subject']."\",\"".$row['post']."\")' >EDIT </a></br>";