2012-05-20 61 views
-1

我有一個XML文件(scores.xml),即時通訊使用PHP代碼添加新的標籤。PHP提交錯誤的代碼到XML

我有一個標籤叫頭包含一些HTML代碼

<![CDATA[<tr><td colspan='7' id='headertd'> 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
<img border='0' src='images/euro.png' /> 
&nbsp;&nbsp;&nbsp;&nbsp; 
UEFA Euro 2012 Qualifications</td></tr>]]> 

當我寫在PGP腳本的形式驗證碼和submiting一切正常,除了標題標籤中的XML文件... 。IM得到一個錯誤的PHP腳本,代碼會在那樣的XML標籤:

&lt;![CDATA[&lt;tr&gt;&lt;td colspan='7' id='headertd'&gt;&#13; 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#13; 
&lt;img border='0' src='images/euro.png' /&gt;&#13; 
&nbsp;&nbsp;&nbsp;&nbsp;&#13; 
UEFA Euro 2012 Qualifications&lt;/td&gt;&lt;/tr&gt;]]&gt; 

因此多數民衆贊成讓錯誤的信息給我的XML ... 有反正我可以解決這一問題?並避免這些代碼的轉換?

這就是我的PHP代碼:

<?php 
if (isset($_POST['submitted'])) {//If the user submitted the form, then add to the XML file 
    //Load the scores XML file 
    $scores = new DOMDocument(); 
    $scores -> load('../scores.xml'); 

    //Get the <Games> tag 
    $games = $scores -> getElementsByTagName('Games'); 

    //Create the new <Game> tag 

    $newGame = $scores -> createElement("Game"); 
    $newGame -> appendChild($scores -> createElement("Header", $_POST['header'])); 

    //Add the new <Game> tag under the <Games> tag 
    $games -> item(0) -> appendChild($newGame); 

    //Save again 
    $scores -> save('../scores.xml'); 

    echo "New game added."; 
} 
?> 

<form id="form1" method="post" action=""> 
Header: <textarea style=" color:#000;" name="header" cols="73" rows="6" > </textarea> 
<br><input type="submit" name="submitted" name="Add new row"> 
</form> 

我有沒有用戶界面的即時通訊只是使用該腳本,使工作更容易爲我張貼在我的網站上的東西!

您的幫助非常感謝!

在此先感謝!

+1

是更好嗎?:) – Chta7

回答

2

嗯,對不起,我正在考慮它,但PHP腳本將它轉換爲「編碼」形式是非常正常的,因爲當你添加html標籤時,那麼xml結構將被破壞,所以它必須被編碼。因此,稍後當您嘗試檢索數據時,必須使用html_entity_decode()將其解碼,才能將其正確導出到瀏覽器。

正如我所看到的,內容是「編碼」的,所以在保存數據之前用html_entity_decode()解碼。 而這裏的「最終」代碼:

<?php 
if (isset($_POST['submitted'])) {//If the user submitted the form, then add to the XML file 
    //Load the scores XML file 
    $scores = new DOMDocument(); 
    $scores -> load('../scores.xml'); 

    //Get the <Games> tag 
    $games = $scores -> getElementsByTagName('Games'); 

    //Create the new <Game> tag 

    $newGame = $scores -> createElement("Game"); 
    $newGame -> appendChild($scores -> createElement("Header", html_entity_decode($_POST['header']))); 

    //Add the new <Game> tag under the <Games> tag 
    $games -> item(0) -> appendChild($newGame); 

    //Save again 
    $scores -> save('../scores.xml'); 

    echo "New game added."; 
} 
?> 

<form id="form1" method="post" action=""> 
Header: <textarea style=" color:#000;" name="header" cols="73" rows="6" > </textarea> 
<br><input type="submit" name="submitted" name="Add new row"> 
</form> 
+0

你可以告訴我的代碼嗎?以及如何使用它?我不知道很多關於PHP的知識,我在製作這個腳本時得到了幫助:)謝謝! – Chta7

+0

完成,希望這有幫助... – HamZa

+0

我改變了這個'$ newGame - > appendChild($ scores - > createElement(「Header」,html_entity_decode($ _ POST ['header'])));'as u u wrote .. 。但它沒有工作了:(我用一個簡單的html代碼'

​​東西
'嘗試過了,它是在XML寫爲'<表> <TR> <TD>東西</TR > </TD > < /表>' – Chta7

1

嘗試對$_POST['header']內容使用html_entity_decode把它放在<Game>標記之前。

+0

可以告訴我如何寫它請!因爲我不知道很多關於PHP的知識!謝謝! – Chta7

+0

上面的例子是你正在尋找 – bjauy

+0

thx試圖幫助我的隊友我發現它! – Chta7