doulilou8560 2013-03-09 15:53
浏览 74
已采纳

php + ajax +评论系统错误[关闭]

i am creating a comment system using php mysql and ajax but keep geting a syntax error and i can not find the dam error this is the comments.php code if anyone help me a will appreciate that

comments.php

<?php

mysql_connect("localhost","root",""); //connect to the database: change user and pass to your own DB's user and pass
mysql_select_db("ajaxComment"); //change this to the name of your mysql database
if(isset($_POST['type'] == "addcomment")) {
    $author = mysql_real_escape_string($_POST['author']);
    $message = mysql_real_escape_string($_POST['message']);
    //the mysql_real_escape_string() is to prevent sql injections and such.
    if($author == "" || $message == "") { //one of the fields was empty...
        die("<p><font color='red'>Error: Please fill out BOTH fields.</font></p>"); //...tell them
    }
    $q1 = mysql_query("INSERT INTO `comments` (`author`, `message`) VALUES ('$author', '$message')") or die("<p>Mysql Error: <b>".mysql_error()."</b></p>"); //if there was a mysql error, let them know
    echo "<p><font color='green'>Comment added successfully. You should see it in a few seconds.</font></p>";
} elseif(isset($_POST['type'] == "getcomments")) { //ajax wants the comments.
    $q1 = mysql_query("SELECT * FROM `comments`");
    $n1 = mysql_num_rows($q1);
    if($n1 == 0) { //no comments found
        die("<p><font color='red'>No comments were found.</font></p>");
    }
    echo "<table border=1>";
    echo "<tr><td><b>Author</b></td><td><b>Comment</b></td></tr>";
    while($r1 = mysql_fetch_assoc($q1)) { //if there were any comments, loop through them
        $a = $r1['author'];
        $m = $r1['message'];
        echo "<tr><td>$a</td><td>$m</td></tr>";
    }
    echo "</table>";
} else {
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Ajax Comment Tutorial</title>
<script src="http://code.jquery.com/jquery-1.4.2.min.js" type="text/javascript"></script>
<!--The above code will get the jquery AJAX library and include it into the page-->
<script type="text/javascript">
(document).ready(function() {
    $("submit").click(function(){ //this will execute code if the submit button was clicked
        var user = $("#author").val(); //receive the contents of the field with the ID "author" and put it into the user var
        var comment = $("#message").val(); //same as above, but with the message field instead.
        $.post("comments.php",{type: "addcomment",author: user,message: comment},function(data){$("#return").html(data)});
        //the first parameter of the above, is the page to send the post data too. In this example, this page is called comments.php, so we send it there.
        //the second parameter is the post data, it goes: {pn: pd} where pn is the name you use in PHP's $_POST and pd is the data it will contain.
        //the third parameter is for the return data, in this case we sent it to the "return" div we made below our form. The first two parameters are not required.
        return false; //this will stop the page submitting, so it won't refresh.
    });
});
    function getComments() { //this function will collect the comments
        $.post("comments.php",{type: "getcomments"},function(data){$("#comments").html(data)});
        //for an explanation on this, simply read the click function above.
    }
        setInterval("getComments()",10000); //gets the comments every 10 seconds.
</script>
</head>

<body>
    <div style="font-size: 18px;">
        <p>Current Comments: </p>
        <div id="comments"></div> <!--This div will be filled with AJAX-->
        <hr />
        <p>Add a comment:</p>
        <form action="comments.php" method="post">
            <p>Username: <input type="text" name="author" id="author" /></p>
            <p>Comment: <textarea name="message" cols="70" rows="10" id="message"></textarea></p>
            <p><input type="submit" name="submit" value="Add Comment" id="submit" /></p>
        </form>
        <div id="return"></div> <!--This div will be filled with the return data from the comment processing script at the top-->
    </div>
</body>
</html>
<?php }  ?>
  • 写回答

1条回答 默认 最新

  • doyhq66282 2013-03-09 15:57
    关注

    PHP error is in this line:

    if(isset($_POST['type'] == "addcomment")) {
    

    you probably want it to be:

    if (isset($_POST['type']) && $_POST['type'] == "addcomment") {
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。