douyou7797 2016-04-23 01:35
浏览 39
已采纳

Php添加到html文件

How can I use PHP to edit a html file on a website? To explain what I want to do, I want to have a php file with input boxes to add to a list in html.

So admin.php will edit index.html by adding more contents to the list.

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

https://jsfiddle.net/dam30t9p/

  • 写回答

2条回答 默认 最新

  • douxu5845 2016-04-23 02:01
    关注

    As I mentioned in my comment, I recommend you create a form that then saves this information (in a database, text file, or other storage option), and then another php file would extract that information. Since I believe you are new to programming, I will explain how to do it with a text file, however I HIGHLY recommend you use a database to store the information, not just because of it's speed in executing queries, but for securely storing information that might be sensative.

    Form page: 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 page that saves the information: 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."
    ");
    fwrite($myFile, $message2."
    ");
    fclose($myFile);
    
    ?>
    

    Then in another php file you would retrieve that information and use it in your website:

    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>
    

    Let me know if that helped!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了