我有一個頁面,我試圖給它添加一個ajax評論系統。當我將/ comment目錄中的所有代碼放入根目錄時,我的新頁面可以實現該腳本。但是,如果我創建另一個目錄,請說/ books,然後鏈接到/ comment目錄中的頁面,它不會發布評論。我可以顯示它們並訪問javascript頁面,但我不能提出新的評論。是什麼導致它失敗。我認爲它是在JavaScript文件中的某個地方......如果您需要查看其他內容,我不想包含太多的代碼,請告訴我,我會發布它。我將把php文件和javascript文件包含在一個目錄中,然後放到另一個目錄中......任何提示都會很棒。這裏是我的網頁:如何讓ajax評論系統從不同的目錄工作?
<?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');
});
});
提交路徑是問題。非常感謝你! – Paxwell 2012-04-25 14:43:14
我改變了提交路徑,看看其他人解決方案是否實際解決了問題。它是。所以,他拿蛋糕。 – Paxwell 2012-04-25 14:47:31
哈哈im很抱歉,但我忘了保存它。你是真正的解決方案。我很抱歉。 – Paxwell 2012-04-25 14:49:10