Php 如何让ajax评论系统在不同的目录下工作?
我有一个页面,我正在尝试向其中添加一个ajax评论系统。当我将/comment目录中的所有代码放到根目录中时,我的新页面可以实现脚本。但是,如果我创建另一个目录,比如/books,然后链接到/comment目录中的页面,它将不会发布评论。我可以显示它们并访问javascript页面,但我不能发表新的评论。是什么导致它失败的。我想它在javascript文件的某个地方。。。我不想包括这么多的代码,如果你需要看到任何其他东西,让我知道,我会张贴它。我将把php文件和javascript文件放在一个目录中,然后放在另一个目录中。。。任何提示都很好。这是我的页面:Php 如何让ajax评论系统在不同的目录下工作?,php,javascript,mysql,html,ajax,Php,Javascript,Mysql,Html,Ajax,我有一个页面,我正在尝试向其中添加一个ajax评论系统。当我将/comment目录中的所有代码放到根目录中时,我的新页面可以实现脚本。但是,如果我创建另一个目录,比如/books,然后链接到/comment目录中的页面,它将不会发布评论。我可以显示它们并访问javascript页面,但我不能发表新的评论。是什么导致它失败的。我想它在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');
});
});
您似乎正在使用相对路径通过include连接到数据库:
include('../comments/connect.php');
这将是第一件需要改变的事情,可能是:
include($_SERVER['DOCUMENT_ROOT'] . '/comments/connect.php');
通常,查找相对路径,看看是否可以将其更改为绝对路径,对于php文件,可以是相对于服务器根目录的路径,对于javascript文件,可以是相对于web根目录的路径。似乎您正在使用相对路径通过包含连接到数据库:
include('../comments/connect.php');
这将是第一件需要改变的事情,可能是:
include($_SERVER['DOCUMENT_ROOT'] . '/comments/connect.php');
一般来说,查找相对路径,看看是否可以将它们更改为绝对路径,无论是相对于php文件的服务器根目录还是相对于javascript文件的web根目录。我认为问题在于javascript方面,您的帖子指向相对url,您应该用绝对url替换它:
/* Sending the form fileds to submit.php: */
$.post('submit.php',$(this).serialize(),function(msg){//<- replace submit.php with absolute url
...
我认为问题在于javascript方面,您的帖子指向相对url,您应该将其替换为绝对url:
/* Sending the form fileds to submit.php: */
$.post('submit.php',$(this).serialize(),function(msg){//<- replace submit.php with absolute url
...
您的评论脚本通常位于一个不变的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
希望这对您构建工作解决方案有足够的帮助。您的注释脚本通常位于一个不变的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
希望这对您构建一个有效的解决方案有足够的帮助。这似乎不是问题,我正在建立所有连接,但由于某些原因,我无法将表单数据传递到submit.php并继续传递到数据库中。我不认为这会解决我的问题。不得不说,这实际上是submit.php中的文档根。我把绝对值放在顶部,这就解决了它@Paxwell很高兴你修好了!没关系,正是上面的回答解决了这个问题。javaw中的路径。作为断裂点。这条路在走。我刚换了他的背就忘了存钱。当我这样做的时候,它又坏了。但是谢谢。我也实现了这一点,这似乎不是问题所在,我正在建立所有连接,但由于某些原因,我无法将表单数据传递到submit.php并继续传递到数据库中。我不认为这会解决我的问题。不得不说,这实际上是submit.php中的文档根。我把绝对值放在顶部,这就解决了它@Paxwell很高兴你修好了!没关系,正是上面的回答解决了这个问题。javaw中的路径。作为断裂点。这条路在走。我刚换了他的背就忘了存钱。当我这样做的时候,它又坏了。但是谢谢。我实现了这个,提交路径就是问题所在。非常感谢你!我更改了提交路径,只是想看看其他人的解决方案是否真的解决了这个问题。的确如此。那么,h
我很抱歉,但是我忘了保存它。你才是真正的解决方案。很抱歉,提交路径是问题所在。非常感谢你!我更改了提交路径,只是想看看其他人的解决方案是否真的解决了这个问题。的确如此。所以,他拿走了蛋糕。哈哈,我很抱歉,但是我忘了保存它。你才是真正的解决方案。很抱歉,这很有帮助,但不是我想要的。谢谢+1@Paxwell我知道。发帖后我意识到我有点偏离正轨了。很抱歉你的解决方案出奇地优雅:D,我喜欢voteup,但可能会让新手感到困惑。这很有帮助,但不是我想要的。谢谢+1@Paxwell我知道。发帖后我意识到我有点偏离正轨了。很抱歉你的解决方案出奇地优雅:D,我喜欢它,但可能会让新手感到困惑。