我使用get_the_content
传递到javascript变量,然后返回到另一个PHP函数。这很好,但在使用get_the_content
. 如果帖子只包含一行(没有换行符),那么效果很好。但如果帖子包含换行符,我会在控制台中看到这个错误:
SyntaxError: \'\' string literal contains an unescaped line break
使用
the_content
在这个应用程序中对我不起作用,因为它根本不返回任何内容。
如何解决此问题?
编辑:
这是我在单击按钮时将PHP变量传递给Javascript的方式:
onclick="searchEmail(\'<?php echo the_author_email(); ?>\', \'<?php echo the_title(); ?>\', \'<?php echo get_the_content(); ?>\', \'<?php echo $location ?>\');"
这是javascript:
function searchEmail(email,title,content,location) {
var $url = (admin_ajax.url);
$.ajax({
type: "POST",
url: $url,
datatype: "html",
data: { \'action\': \'search_notify_email\', email: email, title: title, content: content, location: location },
success: function() {
searchNotification();
},error:function() {
searchNotificationError();
}
});
}
最后,这是PHP函数,它从Javascript接收变量并通过电子邮件发送:
function search_notify_email() {
// Set variables
$email = $_POST[\'email\'];
$title = $_POST[\'title\'];
$content = $_POST[\'content\'];
$location = $_POST[\'location\'];
// Change Email to HTML
add_filter( \'wp_mail_content_type\', \'set_email_content_type\' );
$to = $email;
$subject = "Test subjest";
$message = "<img src=\'https://example.com/favicon.png\'><br><b>Test!</b>";
if (empty($title)) {
$message .= "<br><br><b>" . $_POST[\'content\'] . "</b> test.<br> ";
}
else {
$message .= "<br><br><b>" . $_POST[\'content\'] . "</b> test " . $_POST[\'title\'] . " test.<br> ";
}
if (!empty($location)) {
$message .= "Test <b>" . $_POST[\'location\'] . "</b>";
}
$headers[] = \'From: Test <[email protected]>\';
if ( wp_mail($to, $subject, $message, $headers) ) {
// Success
} else {
// Error
}
die();
// Remove filter HTML content type
remove_filter( \'wp_mail_content_type\', \'set_email_content_type\' );
}
add_action(\'wp_ajax_nopriv_search_notify_email\', \'search_notify_email\');
add_action(\'wp_ajax_search_notify_email\', \'search_notify_email\');
// Reset Email content type to standard text/html
function set_email_content_type() {
return \'text/html\';
}
SO网友:user141080
问题是JavaScript。在JavaScript中,如果不转义换行符,就不能跨越多行的字符串。
var longString = \'This is a very long string which needs
to wrap across multiple lines because
otherwise my code is unreadable.\';
// SyntaxError: unterminated string literal
您可以尝试用+运算符、反斜杠或模板文字(ECMAScript 2015)替换换行符。
带+运算符的文本:
var longString = \'This is a very long string which needs \' +
\'to wrap across multiple lines because \' +
\'otherwise my code is unreadable.\';
带反斜杠的文本:
var longString = \'This is a very long string which needs \\
to wrap across multiple lines because \\
otherwise my code is unreadable.\';
具有模板文本的文本:
var longString = `This is a very long string which needs
to wrap across multiple lines because
otherwise my code is unreadable.`;
资料来源:
SyntaxError: unterminated string literal - MDN web docsEdit:
要替换新行,可以使用以下代码:
<?php
// test string
$str = "This is a very long string which needs
to wrap across multiple lines because
otherwise my code is unreadable.";
function replace_with_plus($str)
{
$str_array = explode ( "\\n" , $str );
$len = count( $str_array );
$str_2 = \'\';
for($i = 0; $i < $len; $i ++)
{
$line = $str_array[$i];
if($i > 0 )
{
$str_2 .= "\'";
}
$str_2 .= $line;
if($i < $len - 1 )
{
$str_2 .= "\' + ";
}
}
return $str_2;
}
?>
<script>
// replace the \\n with the + operator
var test2 = \'<?php echo replace_with_plus($str); ?>\';
</script>
浏览器中的输出:
<script>
// replace the \\n with the + operator
var test2 = \'This is a very long string which needs \' + \'to wrap across multiple lines because \' + \'otherwise my code is unreadable.\';
</script>
正如tmdesign所说,将内容放入JavaScript值的整个想法确实有问题,不是最好的方式。
我强烈建议你使用捷克宫在评论中解释的方式。