【问题标题】:How to get ajax commenting system to work from different directory?如何让 ajax 评论系统从不同的目录工作?
【发布时间】:2012-04-25 14:04:19
【问题描述】:

我有一个页面,我正在尝试向它添加一个 ajax 评论系统。当我将 /comment 目录中的所有代码放入根目录时,我的新页面可以实现该脚本。但是,如果我创建另一个目录,例如 /books,然后链接到 /comment 目录中的页面,它不会发布 cmets。我可以显示它们并访问 javascript 页面,但我不能不制作新的 cmets。是什么导致它失败。我认为它在 javascript 文件中的某个位置...如果您需要查看其他内容,我不想包含这么多代码,请告诉我,我会发布它。我会将 php 文件和 javascript 文件包含在一个目录中,然后放入另一个目录中……任何提示都会很棒。这是我的页面:

<?php

// Error reporting:
error_reporting(E_ALL^E_NOTICE);


include('../comments/connect.php');
include($_SERVER['DOCUMENT_ROOT'] . '/comments/comment.class.php');



/*
/   Select all the comments and populate the $comments array with objects
*/

$comments = array();
$result = mysql_query("SELECT * FROM comments ORDER BY id ASC");

while($row = mysql_fetch_assoc($result))
{
    $comments[] = new Comment($row);
}

?>

<html>
<head>
<link rel="stylesheet" type="text/css" href="../style.css" />
</head>
<body>

<ul id="nav">
    <li class="current"><a href="index.html">Home</a></li>
    <li><a href="#"></a>
        <ul>
            <li><a href="#"></a></li>
</ul>
<div id="container">

    <div id="content">

<?php

/*
/   Output the comments one by one:
*/

foreach($comments as $c){
    echo $c->markup();
}

?>

<div id="addCommentContainer">
    <p>Add a Comment</p>
    <form id="addCommentForm" method="post" action="">
        <div>
            <label for="name">Your Name</label>
            <input type="text" name="name" id="name" />

            <label for="email">Your Email</label>
            <input type="text" name="email" id="email" />

            <label for="url">Website (not required)</label>
            <input type="text" name="url" id="url" />

            <label for="body">Comment Body</label>
            <textarea name="body" id="body" cols="20" rows="5"></textarea>

            <input type="submit" id="submit" value="Submit" />
        </div>
    </form>
</div>
</p>

    </div>
</div>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript" src="../comments/script.js"></script>
</body>
</html>

还有 javascript...

$(document).ready(function(){
    /* The following code is executed once the DOM is loaded */

    /* This flag will prevent multiple comment submits: */
    var working = false;

    /* Listening for the submit event of the form: */
    $('#addCommentForm').submit(function(e){

        e.preventDefault();
        if(working) return false;

        working = true;
        $('#submit').val('Working..');
        $('span.error').remove();

        /* Sending the form fileds to submit.php: */
        $.post('submit.php',$(this).serialize(),function(msg){

            working = false;
            $('#submit').val('Submit');

            if(msg.status){

                /* 
                /   If the insert was successful, add the comment
                /   below the last one on the page with a slideDown effect
                /*/

                $(msg.html).hide().insertBefore('#addCommentContainer').slideDown();
                $('#body').val('');
            }
            else {

                /*
                /   If there were errors, loop through the
                /   msg.errors object and display them on the page 
                /*/

                $.each(msg.errors,function(k,v){
                    $('label[for='+k+']').append('<span class="error">'+v+'</span>');
                });
            }
        },'json');

    });

});

【问题讨论】:

    标签: php javascript mysql html ajax


    【解决方案1】:

    您似乎正在使用相对路径通过包含连接到您的数据库:

    include('../comments/connect.php');
    

    首先要改变的是,可能是这样的:

    include($_SERVER['DOCUMENT_ROOT'] . '/comments/connect.php');
    

    一般来说,寻找相对路径,看看是否可以将它们更改为绝对路径,相对于 php 文件的服务器根目录或相对于 web 根目录的 javascript 文件。

    【讨论】:

    • 这里似乎不是问题
    • 我正在建立所有连接,但由于某种原因,我无法将表单数据传递给 submit.php 并继续传递到数据库中。我认为这不会解决我的问题..
    • 不得不说它实际上是 submit.php 中的文档根,我将绝对值放在顶部并修复了它!
    • @Paxwell 很高兴你把它修好了!
    • 没关系,是上面的人回答修复了它。 java w 中的路径。作为突破点。这些路径正在工作。我只是换了背后忘记保存了。当我这样做时,它又坏了。不过谢谢。我也实现了这一点
    【解决方案2】:

    您的评论脚本通常位于不会更改的 URL 上,即 www.domain.com/cmets。然后,您可以使用 GET 请求获取页面的 cmets(通过查询字符串参数指定页面、URL 或其他唯一标识符),然后可以使用 POST 请求发表评论。

    这样,您的 cmets 模块与您的应用程序完全分离,如果您需要更改数据库详细信息或文件路径,则无需检查它包含的每个脚本。

    最简单的情况是,您的 cmets 脚本可以有一个如下所示的 PHP 文件:

    <?php
    
    header('Content-Type: application/json');
    
    switch (strtolower($_SERVER['REQUEST_METHOD'])) {
        case 'get':
            // return comments for page in JSON format
        break;
        case 'post':
            // post new comment; return result in JSON format
        break;
    }
    

    然后在您的 HTML 视图文件中:

    <!DOCTYPE html>
    <html>
      <body>
        <div id="comments"></div>
        <form action="http://domain.com/comments.php" method="post" id="new-comment">
          <!--rest of your form here-->
        </form>
        <script src="jquery.js"></script>
        <script>
          $(document).ready(function() {
            $.getJSON('http://domain.com/comments.php?page_id=YOUR_PAGE_ID', function(comments) {
              $.each(comments, function(index, comment) {
                // add comment to #comments div
              });
            });
    
            $('#new-comment').submit(function() {
              $.post('http://domain.com/comments.php', $(this).serialize(), function(response) {
                // act on your form depending is response was success or not
              });
              return false;
            });
          });
        </script>
      </body>
    </html>
    

    您也可以将上述内容包装在一个插件中,然后使用单线将您的 cmets 小部件添加到您的页面,即$('#comments').nameOfYourCommentsPlugin();。

    希望这对您构建有效的解决方案有足够的帮助。

    【讨论】:

    • 这很有帮助,虽然不是我想要的。谢谢。 +1
    • @Paxwell 我知道。发布后我意识到我有点切线。对不起!
    • 您的解决方案出奇地优雅 :D,我喜欢它voteup,但可能会让新手感到困惑。
    【解决方案3】:

    我认为问题出在 javascript 方面,您的帖子转到相对 url,您应该用绝对 url 替换它:

    /* Sending the form fileds to submit.php: */
    $.post('submit.php',$(this).serialize(),function(msg){//<- replace submit.php with absolute url
    ...
    

    【讨论】:

    • 提交路径是问题所在。非常感谢!
    • 我将提交路径改回来只是为了看看其他人的解决方案是否是真正修复它的方法。确实如此。所以,他拿走了蛋糕。
    • 大声笑我很抱歉,但我忘了保存它。你是真正的解决方案。我很抱歉。
    猜你喜欢
    • 2016-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-15
    • 1970-01-01
    相关资源
    最近更新 更多