2012-06-14 49 views
1

我寫了一個顯示紅色錯誤的php代碼。但不知何故,這似乎並沒有解決。這是我的代碼:回顯字體顏色輸出

<?php 
...  
if(!$name || !$email || !$contact || !$itemid){ 
         //if not display an error message 
         echo "<span style="color: red;" /><center>Fields marked with <strong>&#40; &#42; &#421</strong> are mandatory!</center></span>"; 
         }else{... 


?> 
+0

您可能會看到,如果問題你看看b頁面的HTML頁面的源代碼eing輸出。這意味着作爲疑難解答提示。 – codewaggle

+0

這應該給一個錯誤開始。修理它。 – Gordon

+0

你是對的,不需要看源代碼,如果有錯誤報告,它就在頁面上。看起來像這樣:'解析錯誤:語法錯誤,意外的T_STRING,期待','或';''這可以通過告訴你問題來幫助解決問題。 – codewaggle

回答

3

做這個類似的東西 -

echo '<span style="color: red;" /><center>Fields marked with <strong>&#40; &#42; &#421</strong> are mandatory!</center></span>'; 

""報價是矛盾

+0

謝謝。有效。 – xan

0

您正在生成無效的HTML。 A <span>元素不能包含<center>元素。瀏覽器可能會通過移動<center>元素來糾正錯誤,使其位於<span>之外(因此應用於該跨度的樣式規則不會侵入<center>元素)。

生成有效標記(並將樣式規則移至樣式表)。

echo "<p class='error'>Fields marked with <strong>&#40; &#42; &#421</strong> are mandatory!</p>"; 
+2

從技術上講,不,但我確定那不是造成他的問題的原因。 – xbonez

+1

標籤即使在html 4.0中也不推薦使用;考慮在CSS中使用「文本對齊:中心」。 – 2012-06-14 05:58:21

0

您的報價有衝突。無論是逃避他們,或者使用單引號和雙引號

echo '<span style="color: red;" /><center>Fields marked with <strong>&#40; &#42; &#421</strong> are mandatory!</center></span>'; 
2
<?php 
...  
if(!$name || !$email || !$contact || !$itemid){ 
         //if not display an error message 
         echo "<span style='color: red;' /><center>Fields marked with <strong>&#40; &#42; &#421</strong> are mandatory!</center></span>"; 
         }else{... 


?> 

試試這個 你關閉報價

+0

您能否解釋一下您的答案,以便OP能夠知道他的代碼中哪裏錯了。 – Starx

0

修改單曲在顏色屬性中刪除'中心'也是, echo "<span style='color: red;' />

2

問題是您的PHP表達式上的未轉義引號。

echo "<span style="color: red;" />... 
       //^ Right here   

因爲,你的PHP echo說法也開始用相同的報價即"

下面是可以解決這個不同的方式:

  1. 混合使用引號

    echo "<span style='color: red;' />... 
         // Single quote in the HTML part 
    
  2. 逃離報價

    echo "<span style=\"color: red;\" />... 
        // Escape the quotes so that it gets treated as string rather than a modifier 
    
+0

謝謝。我現在會記住這一點。 – xan