根据 php 脚本解析的表单输出创建 html 文件
create html file from form output parsed by php script
我想使用来自表单的输入进行 html 文档更新
目标是能够输入 URL、输入描述、输入创建日期并输出到文件。
我的想法是将 HTML 文件分成几部分,begin.txt newarticle.txt 和 end.txt
然后使用 fopen 和 fwrite 将其拼凑起来。
我确定有更简单的方法,但这就是我目前正在尝试的方法。
<html>
<body bgcolor="#FFFFFF>
<H1>Add new Article</h1>
<form action="newarticle.php" method="post">
Paste the link address
<input type="text" name="url">
</br>
Paste the description:
<input type="text" name="description">
</br>
Paste the date article was released:
<input type="text" name="date">
<p>
<input type=submit value="Create Article">
</form>
</body>
</html>
newarticle.php
<?php
$v1 = $_POST["url"]; //You have to get the form data
$v2 = $_POST["description"];
$v3 = $_POST["date"];
$file = fopen('newarticle.txt', 'w+'); //Open your .txt file
ftruncate($file, 0); //Clear the file to 0bit
$content = $v1. PHP_EOL .$v2. PHP_EOL .$v3;
fwrite($file , $content); //Now lets write it in there
fclose($file ); //Finally close our .txt
die(header("Location: ".$_SERVER["HTTP_REFERER"]));
?>
这给了我三行的输出。
我如何让它创建一个文件,并将其内容格式化为一段实际的代码:
<li><a href=$v1>$v2</a></li>
<li>$v3</li>
如果您不介意 html 的格式始终与同一组元素完全相同,但属性值不同且内部 HTML,您可以使用 heredoc 建立 html。基本上是一个多行字符串。例如:
$v1 = "info from the form";
$v2 = "more info!";
$built = <<<EOF
<li>$v1</li>\n
<li>$v2</li>
EOF;
echo $built;
这将输出:
<li>info from the form</li>
<li>more info!</li>
我想使用来自表单的输入进行 html 文档更新
目标是能够输入 URL、输入描述、输入创建日期并输出到文件。
我的想法是将 HTML 文件分成几部分,begin.txt newarticle.txt 和 end.txt
然后使用 fopen 和 fwrite 将其拼凑起来。
我确定有更简单的方法,但这就是我目前正在尝试的方法。
<html>
<body bgcolor="#FFFFFF>
<H1>Add new Article</h1>
<form action="newarticle.php" method="post">
Paste the link address
<input type="text" name="url">
</br>
Paste the description:
<input type="text" name="description">
</br>
Paste the date article was released:
<input type="text" name="date">
<p>
<input type=submit value="Create Article">
</form>
</body>
</html>
newarticle.php
<?php
$v1 = $_POST["url"]; //You have to get the form data
$v2 = $_POST["description"];
$v3 = $_POST["date"];
$file = fopen('newarticle.txt', 'w+'); //Open your .txt file
ftruncate($file, 0); //Clear the file to 0bit
$content = $v1. PHP_EOL .$v2. PHP_EOL .$v3;
fwrite($file , $content); //Now lets write it in there
fclose($file ); //Finally close our .txt
die(header("Location: ".$_SERVER["HTTP_REFERER"]));
?>
这给了我三行的输出。
我如何让它创建一个文件,并将其内容格式化为一段实际的代码:
<li><a href=$v1>$v2</a></li>
<li>$v3</li>
如果您不介意 html 的格式始终与同一组元素完全相同,但属性值不同且内部 HTML,您可以使用 heredoc 建立 html。基本上是一个多行字符串。例如:
$v1 = "info from the form";
$v2 = "more info!";
$built = <<<EOF
<li>$v1</li>\n
<li>$v2</li>
EOF;
echo $built;
这将输出:
<li>info from the form</li>
<li>more info!</li>