2016-04-23 72 views
0

如何使用PHP編輯網站上的html文件?爲了解釋我想要做什麼,我想要一個帶有輸入框的PHP文件添加到html中的列表。將php添加到html文件

因此,admin.php將通過向列表添加更多內容來編輯index.html。

<body> 
<li> 
<ul>Dog</ul> 
<ul>Cat</ul> 
</li> 
</body> 

https://jsfiddle.net/dam30t9p/

+0

如果將php輸入信息保存在某處(文本文件,數據庫或其他地方),然後使用另一個php文件進行解壓,這會不會更好? – Webeng

回答

0

正如我在我的評論中提到的,我建議你創建一個表單,然後保存這些信息(在數據庫,文本文件或其他存儲選項中),然後另一個php文件將提取該信息。因爲我相信你是編程新手,所以我會解釋如何用文本文件做到這一點,但我強烈建議你使用數據庫來存儲信息,這不僅僅是因爲它在執行查詢時的速度,而且是爲了安全地存儲信息可能是感性的。

窗體頁:index.php文件

<!DOCTYPE html> 
<html> 
    <head></head> 
    <body> 
    <form method="POST" action="action.php"> 
     <input type="text" name="message1"> 
     <input type="text" name="message2"> 
     <input type="submit" value="Submit"> 
    </form> 
    </body> 
</html> 

PHP頁面,並將這些信息保存:action.php的

<?php 

//receiving the values from the form: 
//I also used htmlspecialchars() here just to prevent cross 
//site scripting attacks (hackers) in the case that you 
//echo the information in other parts of your website 
//If you later decide to store the info in a database, 
//you should prepare your sql statements, and bind your parameters 
$message1 = htmlspecialchars($_POST['message1']); 
$message2 = htmlspecialchars($_POST['message2']); 

//saving the values to a text file: info.txt 
$myFile = fopen('info.txt', 'w'); 
fwrite($myFile, $message1."\n"); 
fwrite($myFile, $message2."\n"); 
fclose($myFile); 

?> 

然後在另一個PHP文件,你將檢索到的信息,並在網站中使用它:

使page2.php

<!DOCTYPE html> 
<html> 
    <head></head> 
    <body> 
    <?php 

     $myFile = fopen('info.txt', 'r'); 
     $message1 = fgets($myFile); 
     $message2 = fgets($myFile); 
     fclose($myFile); 

     echo "message1 = ".$message1; 
     echo "message2 = ".$message2; 

    ?> 
    </body> 
</html> 

讓我知道是否有幫助!

+0

@ gavgrif編輯,謝謝你的頭!半冒號實際上是我最常犯的錯字,不僅僅是因爲錯過了它們,而且還偶然地使用了冒號,尤其是因爲我經常在分號之前使用Shift + 0創建')'。 – Webeng

+0

沒有probs - @Webring - 雖然喜歡你的答案 - 這個在action.php中的方法對於將錯誤寫入error.log.txt – gavgrif

1

您應該使用一個數據庫來存儲內容,然後使用PHP - 內容提取出來的數據庫,並將其回聲到頁面中。

你也需要交換UL的李的文檔中:

<body> 
     <ul> 
      <li>Dog</v> 
      <li>Cat</li> 
     </ul> 
    </body> 

例如:

<body> 
     <ul> 
      <?php 
       //method to extract data from the db giving a variable called $content 
       foreach($rows as $row //whatever loop you created) 
       { 
        $content=$row['content']; 
        echo"<li>".$content."</li>"; 
       } 
      ?> 
     </ul> 
    </body> 
+1

句子以大寫字母開頭。 –

+0

所以修正,謝謝你在一個編程論壇無關評論。 @軌道上的輕量級競賽 – gavgrif

+0

這不是一個編程論壇。 –