2011-06-09 46 views
0

我會有從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);  
} 
} 
?> 
+0

它確實是這樣他混合PHP和JavaScript .. – ThiefMaster 2011-06-09 13:43:11

+0

@Tomalak:對不起,我的英語不好,和平甘 – brian 2011-06-09 14:17:14

+0

敏感性絃樂器儘管如此,如果你解決你的問題發佈的解決方案作爲一個答案,而不是一個編輯。 – 2011-06-09 14:20:32

回答

1

好吧,我解決我的問題通過全新的行和分頁符與作用的preg_replace的刪除

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

您在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」。

+0

這仍然是錯誤的。他在調用PHP函數。 – ThiefMaster 2011-06-09 13:40:44

+0

小心$這個 - >是PHP代碼。 – 2011-06-09 13:41:32

+0

這仍然行不通。你需要用''左右'$這個 - >形式 - >渲染()',因爲這是PHP代碼。另一個選項是將整個JQuery腳本用引號括起來,並用PHP中的'echo'。 – js1568 2011-06-09 13:41:43

1

你混合PHP和JavaScript代碼。你總是需要記住,PHP運行在服務器和客戶端的JavaScript上。

你想要什麼大概是這樣的:

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

顯然,你需要確保在該字符串的所有換行符和單引號被轉義(\n\'

+0

很顯然腳本是在函數renderScript中生成的PHP類中的 – brian 2011-06-09 13:53:08

+0

這只是它們中的一小部分 – brian 2011-06-09 13:54:09

0

你混合JavaScript和PHP代碼。

我想,如果是由PHP腳本生成的JS,這可能是更好的:

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

您在呈現表單後忘記了回顯:d – brian 2011-06-09 13:44:21

+0

我不知道渲染是返回一個字符串還是顯示一些東西。 – 2011-06-09 13:46:50

0

PHP是在服務器端解析JavaScript是在客戶端運行之前。因此,PHP將被直接注入到Javascript字符串中。然後JQuery將把它附加到你指定的地方。

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

基本上,當你的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字符串:

​​
相關問題