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