簡體   English   中英

PHP添加到HTML文件

[英]Php to add to html file

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

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

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

https://jsfiddle.net/dam30t9p/

您應該使用數據庫存儲內容,然后使用php-從數據庫中提取內容並將其回顯到頁面中。

另外,您還需要在文檔中交換ul和li:

    <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>

正如我在評論中提到的,我建議您創建一個表單,然后將該信息保存(在數據庫,文本文件或其他存儲選項中),然后另一個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>

讓我知道是否有幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM