Eval()在ajax中无法正常工作 [英] Eval () doesn't works correctly in ajax
问题描述
我有一个带有ajax和mysql的评论系统。我可以连接到数据库,可以留下评论的回复,但不能添加新的评论。注释不会发送到数据库,也不会显示有关成功提交的消息。
index.php
I have a comment system with ajax and mysql. I can connect to the database, can leave responses to comments, but can't add new comments. The comment is not sent to the database and no message appears about the successful submission.
index.php
<html>
<head>
<style>
body {
font-family: Arial;
width: 550px;
}
.comment-form-container {
background: #F0F0F0;
border: #e0dfdf 1px solid;
padding: 20px;
border-radius: 2px;
}
.input-row {
margin-bottom: 20px;
}
.input-field {
width: 100%;
border-radius: 2px;
padding: 10px;
border: #e0dfdf 1px solid;
}
.btn-submit {
padding: 10px 20px;
background: #333;
border: #1d1d1d 1px solid;
color: #f0f0f0;
font-size: 0.9em;
width: 100px;
border-radius: 2px;
cursor:pointer;
}
ul {
list-style-type: none;
}
.comment-row {
border-bottom: #e0dfdf 1px solid;
margin-bottom: 15px;
padding: 15px;
}
.outer-comment {
background: #F0F0F0;
padding: 20px;
border: #dedddd 1px solid;
}
span.commet-row-label {
font-style: italic;
}
span.posted-by {
color: #09F;
}
.comment-info {
font-size: 0.8em;
}
.comment-text {
margin: 10px 0px;
}
.btn-reply {
font-size: 0.8em;
text-decoration: underline;
color: #888787;
cursor:pointer;
}
#comment-message {
margin-left: 20px;
color: #189a18;
display: none;
}
</style>
<title>Comment System using PHP and Ajax</title>
<script src="jquery-3.2.1.min.js"></script>
<body>
<h1>Comment System using PHP and Ajax</h1>
<div class="comment-form-container">
<form id="frm-comment">
<div class="input-row">
<input type="hidden" name="comment_id" id="commentId"
placeholder="Name" /> <input class="input-field"
type="text" name="name" id="name" placeholder="Name" />
</div>
<div class="input-row">
<textarea class="input-field" type="text" name="comment"
id="comment" placeholder="Add a Comment"> </textarea>
</div>
<div>
<input type="button" class="btn-submit" id="submitButton"
value="Publish" /><div id="comment-message">Comments Added Successfully!</div>
</div>
</form>
</div>
<div id="output"></div>
<script>
function postReply(commentId) {
$('#commentId').val(commentId);
$("#name").focus();
}
$("#submitButton").click(function () {
$("#comment-message").css('display', 'none');
var str = $("#frm-comment").serialize();
$.ajax({
url: "comment-add.php",
data: str,
type: 'post',
success: function (response)
{
var result = eval(response); //this line with eval
if (response)
{
$("#comment-message").css('display', 'inline-block');
$("#name").val("");
$("#comment").val("");
$("#commentId").val("");
listComment();
} else
{
alert("Failed to add comments !");
return false;
}
}
});
});
$(document).ready(function () {
listComment();
});
function listComment() {
$.post("comment-list.php",
function (data) {
var data = JSON.parse(data);
var comments = "";
var replies = "";
var item = "";
var parent = -1;
var results = new Array();
var list = $("<ul class='outer-comment'>");
var item = $("<li>").html(comments);
for (var i = 0; (i < data.length); i++)
{
var commentId = data[i]['comment_id'];
parent = data[i]['parent_comment_id'];
if (parent == "0")
{
comments = "<div class='comment-row'>"+
"<div class='comment-info'><span class='commet-row-label'>from</span> <span class='posted-by'>" + data[i]['comment_sender_name'] + " </span> <span class='commet-row-label'>at</span> <span class='posted-at'>" + data[i]['date'] + "</span></div>" +
"<div class='comment-text'>" + data[i]['comment'] + "</div>"+
"<div><a class='btn-reply' onClick='postReply(" + commentId + ")'>Reply</a></div>"+
"</div>";
var item = $("<li>").html(comments);
list.append(item);
var reply_list = $('<ul>');
item.append(reply_list);
listReplies(commentId, data, reply_list);
}
}
$("#output").html(list);
});
}
function listReplies(commentId, data, list) {
for (var i = 0; (i < data.length); i++)
{
if (commentId == data[i].parent_comment_id)
{
var comments = "<div class='comment-row'>"+
" <div class='comment-info'><span class='commet-row-label'>from</span> <span class='posted-by'>" + data[i]['comment_sender_name'] + " </span> <span class='commet-row-label'>at</span> <span class='posted-at'>" + data[i]['date'] + "</span></div>" +
"<div class='comment-text'>" + data[i]['comment'] + "</div>"+
"<div><a class='btn-reply' onClick='postReply(" + data[i]['comment_id'] + ")'>Reply</a></div>"+
"</div>";
var item = $("<li>").html(comments);
var reply_list = $('<ul>');
list.append(item);
item.append(reply_list);
listReplies(data[i].comment_id, data, reply_list);
}
}
}
</script>
</body>
</html>
comment-add.php
comment-add.php
<?php
require_once ("db.php");
$commentId = $_POST['comment_id'];
$comment = $_POST['comment'];
$commentSenderName = i$_POST['name'];
$date = date('Y-m-d H:i:s');
$sql = "INSERT INTO tbl_comment(parent_comment_id,comment,comment_sender_name,date) VALUES ('$commentId','$comment','$commentSenderName','$date')";
$result = mysqli_query($conn, $sql);
if (! $result) {
$result = mysqli_error($conn);
}
echo $result;
?>
comment-list.php它确实有效,但我将代码留在这里
comment-list.php it actually works, but i leave the code here
<?php
require_once ("db.php");
$sql = "SELECT * FROM tbl_comment ORDER BY parent_comment_id asc, comment_id asc";
$result = mysqli_query($conn, $sql);
$record_set = array();
while ($row = mysqli_fetch_assoc($result)) {
array_push($record_set, $row);
}
mysqli_free_result($result);
mysqli_close($conn);
echo json_encode($record_set);
?>
db.php包含数据库连接的数据。除了添加新的评论之外,这一切都很完美。
ps抱歉英语不好
我尝试了什么:
当我试图删除行var result = eval(response);在index.php ajax函数中,有一条关于发送注释的消息,但注释本身没有出现在数据库中。我认为错误是在脚本中发送新评论,并可能归咎于评估。有人看到错误吗?
db.php contains data for database connection. It all works perfectly except for adding new comments.
ps sorry for bad english
What I have tried:
When I tried to remove the line var result = eval(response); in the index.php ajax function, there was a message about sending a comment, but the comment itself did not appear in the database. I think that the error is in the script to send a new comment, and possibly to blame eval. Someone sees a mistake?
推荐答案
('#commentId')。val(commentId);
('#commentId').val(commentId);
(#name )。焦点();
}
("#name").focus(); }
(#submitButton)。click(function(){
("#submitButton").click(function () {
这篇关于Eval()在ajax中无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!