我有简单的评论脚本,可以添加/编辑/删除评论。我对特殊字符有疑问。
如果 var currentMessage 包含 ' 或 "则代码会中断。
我将评论保存在数据库中,如下所示:
$Message = clean_data($_POST["txtmessage"]); 然后执行典型的 mysqli_query 保存....
function clean_data($input) {
$input = trim(htmlentities(strip_tags($input,",")));
if (get_magic_quotes_gpc())
$input = stripslashes($input);
$input = stripslashes($input);
$input = htmlspecialchars($input, ENT_IGNORE, 'utf-8');
$input = trim($input);
$sqllink = new mysqli(MySQL_Host, MySQL_UserName, MySQL_Password, MySQL_Database);
$input = mysqli_real_escape_string($sqllink, $input);
no_magic_quotes($input);
$input = str_replace("'", "'", $input);
$input = str_replace("&", "&", $input);
$input = str_replace('"', """, $input);
$input = strip_tags($input);
return $input;
}
MySQL中保存的数据如下:let's do it!
我的编辑按钮是这样的:
<button id="BtnEdit_<?php echo $r_c['CommentID']; ?>" class="btn btn-xs btn-warning" onClick="showEditBox(this,<?php echo $r_c['CommentID']; ?>)" name="edit"><i class="fas fa-edit"></i> <? echo _("To Edit"); ?></button>
我的消息显示如下:
<div class="message-content"><? echo $r_c['Message']; ?></div>
如果我在 php echo 中显示特殊字符,那么 javascript 将不起作用。如果我删除所有 ' 或 ""那么就可以了。但这并不实用。
function showEditBox(editobj,id) {
$('#BtnEdit_'+ id).hide();
$('#frmAdd').show();
var currentMessage = $("#message_" + id + " .message-content").html();
var editMarkUp = '<input type="text" class="form-control input-sm" name="txtmessage" id="txtmessage_'+id+'" value="'+currentMessage+'"><br><div class="btn-group pull-left"><button class="btn btn-xs btn-success" name="ok" onClick="callCrudAction(\'edit\','+id+')"><i class="fas fa-check"></i> <? echo _("Save"); ?></button> <button class="btn btn-xs btn-default" name="cancel" onClick="cancelEdit(\''+currentMessage+'\','+id+')"><i class="fas fa-times"></i> <? echo _("Cancel"); ?></button></div>';
$("#message_" + id + " .message-content").html(editMarkUp);
}
数据显示如下:让我们开始吧! (如果是的话那就更好了 - 让我们开始吧!)
如果评论中有 ',编辑按钮甚至不起作用。
最佳答案
您不需要通过调用 htmlentities
、strip_tags
、stripslashes
、htmlspecialchars
、mysqli_real_escape_string
或no_magic_quotes
。尽管您没有包含插入数据的代码,但这表明您没有使用准备好的语句插入数据。这正是您应该追求的目标。
它可能看起来像这样:
用 PHP 写入数据库:
$message = $_POST["txtmessage"];
$sqllink = new mysqli(MySQL_Host, MySQL_UserName, MySQL_Password, MySQL_Database);
$stmt = $sqllink->prepare("INSERT INTO messages(message) VALUES(?)");
$stmt->bind_param("s", $message);
$stmt->execute();
$stmt->close();
HTML 生成应该确保在必要时使用 HTML 实体,尤其是 & 符号。所以在这里调用 htmlspecialchars
:
<div class="message-content"><? echo htmlspecialchars($r_c['Message']); ?></div>
JavaScript 按钮点击处理程序不应获取消息中的 html
,而应获取 text
。另外,不要在 HTML 代码段中设置 input
的 value
;而是使用 val()
赋值:
function showEditBox(editobj, id) {
$('#BtnEdit_'+ id).hide();
$('#frmAdd').show();
// Use text():
var currentMessage = $("#message_" + id + " .message-content").text();
// Instead of building HTML with string concatenation, build nodes with jQuery.
// That way you don't have the problem of escaping/encoding
var nodes = [
$("<input>").addClass("form-control input-sm").attr({
type: "text", name: "txtmessage", id: "txtmessage_"+id
}).val(currentMessage), // <-- here is where the value is set
$("<br>"),
$("<div>").addClass("btn-group pull-left").append(
$("<button>").addClass("btn btn-xs btn-success").attr({ name: "ok" }).append(
$("<i>").addClass("fas fa-check"),
"<? echo _("Save"); ?>"
).click(callCrudAction.bind(null, 'edit', id))
),
"\xA0\xA0\xA0\xA0", // non-breaking spaces
$("<button>").addClass("btn btn-xs btn-default").attr({ name: "cancel" }).append(
$("<i>").addClass("fas fa-times"),
"<? echo _("Cancel"); ?>"
).click(cancelEdit.bind(null, currentMessage, id))
];
$("#message_" + id + " .message-content").empty().append(nodes);
}
我没有更改 HTML,但您应该使用 CSS 为按钮添加边距,而不是生成不间断的空格。
关于javascript - php - javascript - ajax 中的特殊字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54044243/