2015-11-16 44 views
-4

我有一個沒有顯示的表格,它顯示了實際的文字。這裏是代碼:php not echoing html

<?php $form ="<form action='log.php' method='post'> 
<table> 
<tr> 
<td>username</td> 
<td>input type='text' name='user' /></td> 
</tr> 
<tr> 
<td>password</td> 
<td>input type='password' name='password' /></td> 
</tr> 
<tr>  
<td></td> 
<td>input type='submit' name='login' vaue='submit' /></td> 
</tr> 
</table> 
</form>"; 
?> 

它不是輸出表或提交按鈕或輸入區域。任何建議爲什麼? 即時通訊使用與Apache安裝,普通表,表格工作和PHP工作,但不是當我嘗試使用PHP輸出HTML它顯示實際的HTML,而不是它的屬性。

+0

因爲剛剛分配字符串'$ form' 。你沒有迴應內容。 –

+0

打印變量'echo $ form;' – Apb

+0

或'print $ form;' –

回答

0

有一些html標記錯誤。輸入標籤html代碼不正確。用此替換並使用echo()方法來打印$ form變量的值。

<?php $form ="<form action='log.php' method='post'> 
<table> 
<tr> 
<td>username</td> 
<td><input type='text' name='user' /></td> 
</tr> 
<tr> 
<td>password</td> 
<td><input type='password' name='password' /></td> 
</tr> 
<tr>  
<td></td> 
<td><input type='submit' name='login' value='submit' /></td> 
</tr> 
</table> 
</form>"; 
echo $form; 
?> 

希望這能解決您的問題。

+0

'vaue',從OP代碼複製的語法錯誤,無效語法。 –

+0

啊這最後它的顯示。可能是導致問題的一些標記錯誤,這很難注意。謝謝。 – Assy

+0

我已經更新了答案。現在它是完美的。 –

1

你的代碼看起來大約好吧,你錯過了一兩件事,這就是以下幾點:

echo $form; 

爲了在您的網頁打印任何東西,你用echo後面的值或變量。

+0

如果我把$ form;我得到空白的白色屏幕 – Assy

+0

'echo $ form;',not'$ form;'。你確定你正確地使用了'echo'嗎? –

+0

你錯過了很多東西。 –

0

使用echo這樣的: -

<?php 
$form ="<form action='log.php' method='post'> 
<table> 
<tr> 
<td>username</td> 
<td><input type='text' name='user' /></td> 
</tr> 
<tr> 
<td>password</td> 
<td><input type='password' name='password' /></td> 
</tr> 
<tr>  
<td></td> 
<td><input type='submit' name='login' value='submit' /></td> 
</tr> 
</table> 
</form>"; 
echo $form; 
?> 

或更改此input這個<input

+0

ahem >>>'vaue' –

+0

感謝您提高我的回答:) @ Fred-ii- –

2

刪除回聲和PHP標籤本身。

<form action='log.php' method='post'> 
<table> 
<tr> 
<td>username</td> 
<td><input type='text' name='user' /></td> 
</tr> 
<tr> 
<td>password</td> 
<td><input type='password' name='password' /></td> 
</tr> 
<tr>  
<td></td> 
<td><input type='submit' name='login' value='submit' /></td> 
</tr> 
</table> 
</form> 
+0

缺少許多''''''vaue' –

+0

仍然缺少一些'<'。 –

+0

@Fred是的你是對的。我沒有想到那個錯誤。大聲笑。 –

1

我的建議是做這樣的事情:

<?php 
function ShowHtml() 
{ 
?> 
<form action='log.php' method='post'> 
    <table> 
     <tr> 
      <td>username</td> 
      <td> 
       <input type='text' name='user' /> 
      </td> 
     </tr> 
     <tr> 
      <td>password</td> 
      <td> 
       <input type='password' name='password' /> 
      </td> 
     </tr> 
     <tr> 
      <td></td> 
      <td> 
       <input type='submit' name='login' value='submit' /> 
      </td> 
     </tr> 
    </table> 
</form> 
<?php 
} 
?> 

這樣就省去了做一個echoprint並且還修復你的語法錯誤(即缺少<標籤和vaue應該是value) 。

此外,在此你可以有一個功能,如:

<?php 
function ShowHtml($html) 
{ 
    if ($html != null || $html != '' || !empty($html)) # Just a check to make sure then html is set ;P 
    { 
     print $html; # could use echo instead, personal preference :) 
        # Could also add more validation to make sure the html tags themselves are valid and correct 
     return true; 
    } 
    return false; 
} 
?> 

或者你可以做(​​保持PHP環繞聲):

<?php 
$form = "<form action='log.php' method='post'> 
    <table> 
     <tr> 
      <td>username</td> 
      <td> 
       <input type='text' name='user' /> 
      </td> 
     </tr> 
     <tr> 
      <td>password</td> 
      <td> 
       <input type='password' name='password' /> 
      </td> 
     </tr> 
     <tr> 
      <td></td> 
      <td> 
       <input type='submit' name='login' value='submit' /> 
      </td> 
     </tr> 
    </table> 
</form>"; 

echo $form; # Or print, your choice :) 
      # Can also use the bracketed way of echo(...) 
      # or print(...) for an added layer of 
      # 'make sure I only print what I want to' 
?> 
+0

Sidenote Sam:'if($ html!= null && html html =「'&&!empty($ html))''應該是'||'s ;-) –

+0

@ Fred-ii-,我使用'&&'只是因爲它提供了額外的安全層,因爲如果符合條件中的任何一個(按給定的順序),那麼函數的處理方式就是這樣** EVERY * *可能性被覆蓋,我喜歡額外的安全性; P –

+0

'&&'檢查AND',我懷疑一個變量是否可以包含'null'AND'nothing'並且是空的,並且使用'&&';來檢查所有這些條件關於它。除非我不抓住;-) –