2016-04-23 68 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

这不是一个编程论坛。 –