我有呼應,其包含的JavaScript內在PHP字符串中轉義html元素以防止無效輸出到瀏覽器?
輸入一個HTML選擇元素在服務器字符串問題
echo '<select name="services" onchange="document.getElementById("services").style.display = this.options.selectedIndex ? "block" : "none";" >';
在瀏覽器輸出
<select "none";"="" :="" "block"="" ?="" services").style.display="this.options.selectedIndex" onchange="document.getElementById(" name="services"></select>
我試圖逃跑字符串使用\字符,但似乎沒有工作。
echo '<select name="services" onchange="document.getElementById(\"services\").style.display = this.options.selectedIndex ? \"block\" : \"none\";" >';
輸出
<select \"none\";"="" :="" \"block\"="" ?="" services\").style.display="this.options.selectedIndex" onchange="document.getElementById(\" name="services"></select>
任何技巧或竅門來解決這個問題呢?
感謝
我強烈建議你學習更多的HTML和字符允許的屬性值。請參閱http://www.w3.org/TR/html4/intro/sgmltut.html#h-3.2.2 – hakre 2012-02-28 16:59:48
@hakre - 字符串內的有效輸入 - http://www.w3schools.com/tags/tag_select.asp - inb4 w3fools bandwagon – kaleeway 2012-02-28 17:02:36
**輸入**無效(在到達PHP字符串之前)。試圖修復它是可能的(ish),但你應該從源頭解決問題。選擇標籤上允許的屬性是無關緊要的(所以你已經鏈接到一個不相關的垃圾,這是一個非常糟糕的問題),問題是值中的字符,這是一個通用的HTML屬性規則 – Quentin 2012-02-28 17:05:57