错误:字符串文字未终止


Error: Unterminated string literal

我有一些从Zend_Form生成的HTML数据。

我想将它附加到div#wrapper元素,但我的尝试引发了一个错误:Unterminated string literal

我该怎么修?

注意:好吧,每个人看起来都像是假设我忘记在代码上放php标签,但它没有。这是我的"真实"代码

<?php
class Admin_Elements_Dialog {
private $_name;
private $_title;
private $_action;
private $_button = null;
private $_content;
private $_options = array();
public function __construct($options) {
    $this->_options = $options;        
}
public function setName($name) {
    $this->_name = $name;
    return $this;    
}
public function setTitle($title) {
    $this->_title = $title;
    return $this;    
}
public function setAction($action) {
    $this->_action = $action;
    return $this;    
}
public function setOpener($button) {
    if ($button instanceof Admin_Elements_Buttons)
        $this->_button = $button;
    return $this;    
}
public function setContent($content) {
    $this->_content = $content;
    return $this;
}
public function renderScript() {
    $html = array();
    $html[] = '$(document).ready(function() {
                    $("body").append(''<div id="' . $this->_name . '" title="' . $this->_title . '" class="ui-helper-hidden">' . $this->_content . '</div>'');
                    var dialog = $("#' . $this->_name . '").dialog(' . Zend_Json::encode($this->_options, false, array('enableJsonExprFinder' => true)) . '); 
                    $("#' . $this->_button->getId() . '").click(function() {
                        $("#' . $this->_name . '").dialog("open");
                        return false;
                    });
                });';
    $html[] = '';
    return join($html, PHP_EOL);    
}
}
?>

您在Javascript中使用了PHP串联运算符.

Javascript的串联运算符是+:

$(document).ready(function() {
     $("div#wrapper").append('<div id="dialog" title="Add new blog">' + <?php echo $this->form->render() ?> + '</div>');
});

此外,PHP代码被逐字插入到您的页面中。如果$this->form->render()没有生成一个真正的引号封装字符串,那么生成的Javascript将无效。

你的意思可能是:

$(document).ready(function() {
     $("div#wrapper").append('<div id="dialog" title="Add new blog"><?php echo $this->form->render() ?></div>');
});

但肯定有更好的方法。这样,你就必须注意那些可逃避现实的字符和换行符。

还有,我忍不住:这是"提前感谢";没有拖尾"d"

您正在混合PHP和JavaScript代码。您需要始终记住,PHP在服务器上运行,Javascript在客户端上运行。

你想要的可能是:

$(document).ready(function() {
     $("div#wrapper").append('<div id="dialog" title="Add new blog"><?php echo $this->form->render(); ?></div>');
});

显然,您需要确保该字符串中的所有换行符和单引号都已转义('n''

好的,我用函数preg_replace 删除了所有新行和分页符,解决了我的问题

preg_replace('/['r'n]+/', null, $this->_content)

您可以混合使用javascript和PHP代码。

我认为,如果你的js是由PHP脚本生成的,这可能会更好:

  <?php /** I'm in PHP */ ?>
  $(document).ready(function() {
     $("div#wrapper").append('<div id="dialog" title="Add new blog"><?php $this->form->render()?></div>');
   });

PHP在服务器端进行解析,然后在客户端运行Javascript。因此,PHP将直接注入到Javascript字符串中。然后JQuery将把它附加到您指定的位置。

$(document).ready(function() {
     $("div#wrapper").append('<div id="dialog" title="Add new blog"><?php echo $this->form->render() ?></div>');
});

基本上,当您的PHP代码在服务器上呈现时,它会解析<?php ?>之间的所有内容。所以当你的代码呈现时,它会得到类似的东西

$(document).ready(function() {
    $("div#wrapper").append('<div id="dialog" title="Add new blog">' + <form></form> + '</div>');
});

请注意,生成的JS代码有一个<form></form>元素,该元素没有括在引号中。这会导致"未终止的字符串文字"错误。

要解决这个问题,你只需要把它括在引号里,比如

$("div#wrapper").append(
    '<div id="dialog" title="Add new blog">' + '<?php $this->form->render(); ?>' + '</div>'
)

或者,您可以将PHP放入JS字符串中:

$("div#wrapper").append(
    '<div id="dialog" title="Add new blog"><?php $this->form->render(); ?></div>'
)