php - AJAX 评论框 -- 未捕获的语法错误 : Unexpected token <

标签 php mysql ajax

我收到错误:

未捕获的语法错误:意外的标记

这里是forumrm.php第1行。

我输入文本并出现此错误。

forumrm.php

第 1 行

<?php require_once $_SERVER['DOCUMENT_ROOT'].'/booking/defines.php'; ?>

connect.php

<?php
$hostname = "localhost";
$username = "root";
$password = "";
$db = "show";
mysql_connect($hostname, $username , $password) OR die ("Unable to connect with the server");
mysql_select_db($db) or die('Cannot connect with the database!');
?>

defines.php

<?php
    define('DOC_ROOT', $_SERVER['DOCUMENT_ROOT']."/booking/");
    define('DS' , DIRECTORY_SEPAPATOR);

    define('MYSQL_DIR' , DOC_ROOT , DS);
    define('MODELS_DIR' , DOC_ROOT , DS);

    require_once MYSQL_DIR . 'connect.php';
?>

forum_insert.php

<?php



    if(isset($_POST['task']) && $_POST['task'] == 'forum_insert')
    {
        require_once $_SERVER['DOCUMENT_ROOT'].'/booking/defines.php';
        $user_id =(int) $_POST['userId'];
        $reply =addslashes(str_replace("\n", "<br>" , $_POST['reply']));

        $std = new stdClass();
        $std->user_id = $user_id;
        $std->reply = $reply;
        $std->username = "jason123";
        $std->profile_img = "/booking/images/user.png";

        require_once MODELS_DIR . 'reply.php';
        if(class_exist('Reply'))
        {
            $replyInfo = Reply::insert($reply, $user_id);   
            if($replyInfo != null)
            {

            }
        }
        echo json_encode($std);

    }
    else
    {
        header('Location: /booking/forumrm.php');
    }
?>

reply.php

<?php

class Reply{
    public static function getReply()
    {

    }

    public static function insert($reply, $user_id)
    {


        $std = new stdClass();
        $std->reply_id =null;
        $std->reply = $reply;
        $std->user_id = (int)$user_id;

        return $std;
    }
    public static function update($data)
    {

    }
    public static function delete($reply_id)
    {

    }
}
?>

forum_insert.js

$(document).ready(function(){

    $('#reply').click(function(){
        reply_click();
    });
});

function reply_click(){

        //text within textarea which the person has entered.
        var text = $('#reply-text').val();
        var user_id = $('#user_id').val();
        var username = $('#username').val();




        if(text.length >0 && user_id != null)
        {
            $('.forum-insert-container').css('border','1px solid #e1e1e1' );
            console.log(text + "User name :" + username + "User id :" + user_id);

            $.post("/booking/forum_insert.php" ,
            {   
                task :"forum_insert",
                userId : user_id,
                reply : text
            }

            )

            .error(
                function()
                {
                    console.log("Error");
                }
            )

            .success(

                function(data)
                {
                    forum_insert(jQuery.parseJSON(data));
                    console.log("ResponseText" +data);

                }
            );



        }
        else{
            $('.forum-insert-container').css('border','1px solid blue' );
            console.log("The text area was empty");
        }


        //remove text from textarea. 
        $('#reply-text').val("");
};      

function forum_insert(data)
{
    var t ='';
    t +='<li class="forum-holder" id="_'+data.reply_id+'">';

        t +='<div class="user-img">';
        t +='<img src="'+data.profile_img+'" class="user-img-pic" />';
        t +='</div>';

        t +='<div class="forum-body">';
        t +='<h3 class="username-field">'+data.username+'</h3>';

        t +='<div class="forum-text">'+data.reply+'</div>';
        t +=' </div>';

        t +='<div class="forum-buttons-holder">';
        t +='<ul>';
        t +='<li class="delete-btn">X</li>';
        t +='</ul>';
        t +='</div>';
        t +='</li>';

        $('.forum-holder-ul').prepend(t);

}

更多详细信息--forumrm.php

<?php require_once $_SERVER['DOCUMENT_ROOT'] .'/booking/defines.php'; ?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Fourm - Running Man </title>
    <link rel="stylesheet" href="css/style.css" type="text/css">
    <link rel="stylesheet" href="css/styles.css">
    <link href="css/layout.css" rel="stylesheet"/>
    <script type="text/javascript" src="js/jquery.js"></script>
    <script type="text/javascript" src="js/forum_insert.js?t=<?php echo time(); ?>"></script>
    <script type="text/javascript" src="js/script.js"></script>
</head>
<body>
    <div id="background">
        <div id="page">
            <div id="header">
                <table>
        <tbody>
        <tr>
            <th width="20%"><a href="index.html" id="logo"><img src="images/iconnn.png" alt="Logo" height="130" width="200"  ></a></th>
            <th width="60%"><img src="images/banner.png" width="500"></th>
        </tr>
        </tbody></table>
                <div id='cssmenu'>
<ul>
   <li class='active'><a href='index.html'><span>Home</span></a></li>
   <li><a href='aboutus.html'><span>About Us</span></a></li>
   <li><a href='varietyshow1.html'><span>Variety Show</span></a></li>
    <li><a href='host.html'><span>Host</span></a></li>
    <li><a href='game.html'><span>Game</span></a></li>
   <li><a href='login.php'><span>Forums</span></a></li>
   <li class='last'><a href='login.php'><span>Shop</span></a></li>
</ul>

</div><br></div>
            <div id="contents">
            <div class="wrapper">

                        <div class="page-data">
                        <h1 style="text-align:center;">Running Man discuss area! </h1>
                         <ul>
                            <li>non-advocacy, links to other forums / discussion boards or commercial website  </li>
                            <li>Cannot write pron or swaer words in forum.</li>
                            <li>Cannot post the personal details.</li>
                          </ul> 
                        </div>
                            <div class="forum-wrapper">
                                <h3 class="forum-title">Discuss Area</h3>

                                    <div class="forum-insert">
                                        <h3 class="who-says">Says : <span>Jason123</span></h3>
                                            <div class="reply-insert-container">
                                                <textarea id="reply-text" class="reply-insert-text"></textarea>
                                            </div>

                                            <div id="reply" class="reply">
                                                POST
                                            </div>
                                    </div>
                                <div class="forum-list">                                    

                                    <ul class="forum-holder-ul">
                                        <?php $forum = array( "a" , "b" , "c" , "d" ); ?>
                                        <?php require_once $_SERVER['DOCUMENT_ROOT'] .'/booking/forum_box.php' ?>
                                    </ul>
                                </div>
                            </div>

                        </div>
                    <input type="hidden" id="user_id" value="1"/>
                     <input type="hidden" id="username" value="jason123"/>
                </div>
                <div id='cssmenu'>
<ul>
   <li class='active'><a href='index.html'><span>Home</span></a></li>
   <li><a href='aboutus.html'><span>About Us</span></a></li>
   <li><a href='varietyshow1.html'><span>Variety Show</span></a></li>
    <li><a href='host.html'><span>Host</span></a></li>
    <li><a href='game.html'><span>Game</span></a></li>
   <li><a href='login.php'><span>Forums</span></a></li>
   <li class='last'><a href='login.php'><span>Shop</span></a></li>
</ul>

</div>
            <div id="footer">
            <p>
                 All Rights Reserved
            </p>
        </div> 
            </div>

        </div>

    </div>

</body>
</html>

forum_insert.php - 更新

<?php



    if(isset($_POST['task']) && $_POST['task'] == 'forum_insert')
    {
        require_once $_SERVER['DOCUMENT_ROOT'].'/booking/defines.php';
        $user_id =(int) $_POST['userId'];
        $reply =addslashes(str_replace("\n", "<br>" , $_POST['reply']));

        $std = new stdClass();
        $std->user_id = $user_id;
        $std->reply = $reply;
        $std->username = "jason123";
        $std->profile_img = "/booking/images/user.png";

        require_once $_SERVER['DOCUMENT_ROOT'].'/booking/reply.php';
        if(class_exist('Reply'))
        {
            $replyInfo = Reply::insert($reply, $user_id);   
            if($replyInfo != null)
            {

            }
        }
        echo json_encode($std);

    }
    else
    {
        echo json_encode(array("yay" => "works"));
    }
?>

最佳答案

我编辑了我的答案以显示一个简单的论坛:

<?php
// CONNECT TO DATABASE.
$hostname = "localhost";
$username = "root";
$password = "";
$db = "show";
$cnx = mysql_connect($hostname, $username , $password) OR die ("Unable to connect with the server");
mysql_select_db($db) or die('Cannot connect with the database!');
?>
<html>
  <head>
    <title>By José Manuel Abarca Rodríguez</title>
    <script type="text/javascript">
<?php
// RETRIEVE ALL MESSAGES.
$dat = mysql_query( "select * from messages",$cnx )
$messages = "";
while ( $reg = mysql_fetch_array( $dat ) )
  $messages .= $reg[ "message" ] . "<br/>";
?>

function load_messages () {
document.getElementById("div_msg").innerHTML = '<?php echo $messages; ?>';
}
    </script>
  </head>
  <body onload="load_messages()">

    <form method="post" action="insert.php">
      <textarea rows="5" name="message" ></textarea>
      <br/>
      <input type="submit" value="Send message"/>
    </form>
    <hr/>
    <br/>
    Posted messages
    <br/>
    <div id="div_msg">
    </div>

  </body>
</html>

我们将之前的代码保存为“forum.php”,现在是“insert.php”:

<?php
$hostname = "localhost";
$username = "root";
$password = "";
$db = "show";
$cnx = mysql_connect($hostname, $username , $password) OR die ("Unable to connect with the server");
mysql_select_db($db) or die('Cannot connect with the database!');

$message = $_POST["message"]; // THIS DATA COMES FROM THE FORM.
mysql_query( "insert into messages (message) values ('" . $message . "')",$cnx );
header( "Location: forum.php" );
?>

这是如何做到这一点的一般想法。

关于php - AJAX 评论框 -- 未捕获的语法错误 : Unexpected token <,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29259177/

相关文章:

php - 从文件导入sql语句时的编码问题

javascript - 如何使用 Promise all() 同时执行两个函数?

ajax - PrimeFaces 对话框仅在第一次出现时进行验证

Mysql - 使用 DAYOFWEEK 在特定日期的每小时范围内的行

PHPBB 主页集成?

javascript - 在样板文件中实现 Ajax

javascript - 无法从通过 php 发送的 ajax 响应更新 div 文本

php - 使用 PHP 解析 HTML 以从 ids 和 classes 属性返回 CSS 规则

php - 为 imagejpeg/imagepng 函数创建的图像指定缓存验证器

php - 通过 Instagram API 确定媒体删除的有效方法