簡體   English   中英

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

新文章.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>

暫無
暫無

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

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