1

ページがあり、それにajaxコメントシステムを追加しようとしています。/ commentディレクトリ内のすべてのコードをrootに配置すると、新しいページでスクリプトを実装できます。ただし、別のディレクトリ、たとえば/ booksを作成し、/ commentディレクトリ内のページにリンクすると、コメントは投稿されません。それらを表示してjavascriptページにアクセスすることはできますが、新しいコメントを作成することはできません。失敗の原因は何ですか。javascriptファイルのどこかにあると思います...他に何かを見る必要がある場合は、それほど多くのコードを含めたくありませんでした。お知らせください。投稿します。phpファイルとjavascriptファイルを1つのディレクトリに含め、別のディレクトリに含めます...ヒントがあれば素晴らしいと思います。これが私のページです:

<?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');

    });

});
4

3 に答える 3

3

相対パスを使用して、インクルードを使用してデータベースに接続しているようです。

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

それが最初に変更されることであり、次のようなものになる可能性があります。

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

一般に、相対パスを探し、それらを絶対パスに変更できるかどうかを確認します。phpファイルの場合はサーバーのルートを基準にして、javascriptファイルの場合はwebルートを基準にします。

于 2012-04-25T14:09:28.603 に答える
3

通常、コメントスクリプトは変更されないURL、つまりwww.domain.com/commentsにあります。次に、GETリクエスト(クエリ文字列パラメーターを介してページ、URL、またはその他の一意の識別子を指定)を使用してページのコメントをフェッチし、POSTリクエストを使用してコメントを投稿する機能を使用できます。

このように、コメントモジュールはアプリケーションから完全に分離されており、データベースの詳細やファイルパスを変更する必要がある場合に、コメントモジュールに含まれるすべてのスクリプトを実行する必要はありません。

最も簡単な方法では、コメントスクリプト用に次のような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>

上記をプラグインでラップしてから、コメントウィジェットをワンライナーでページに追加することもできます$('#comments').nameOfYourCommentsPlugin();

うまくいけば、それはあなたが実用的なソリューションを構築するのに十分役立つでしょう。

于 2012-04-25T14:21:50.800 に答える
2

問題はjavascript側にあると思います。投稿は相対URLに移動し、絶対URLに置き換える必要があります。

/* Sending the form fileds to submit.php: */
$.post('submit.php',$(this).serialize(),function(msg){//<- replace submit.php with absolute url
...
于 2012-04-25T14:13:25.103 に答える