简体   繁体   中英

create html file from form output parsed by php script

I want to make an html document update with input from a form

The goal is to be able to enter the URL, enter the description, enter the date created and output to a file.

My thoughts are to break the HTML document into pieces, begin.txt newarticle.txt and end.txt

Then piece it back together using fopen and fwrite.

I'm sure there's an easier way, but this is how I'm currently trying to do it.

<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"]));
?>

This gives me the output on three separate lines.

How do I have it create a file with content formatted it into an actual piece of code :

<li><a href=$v1>$v2</a></li>
<li>$v3</li>

If you don't mind the format of the html always being exactly the same with the same set of elements, but different attribute values and inner HTML, you can use a heredoc to build up the html. Basically a multi-line string. For example:

$v1 = "info from the form";
$v2 = "more info!";

$built = <<<EOF
<li>$v1</li>\n
<li>$v2</li>
EOF;

echo $built;

this will output:

<li>info from the form</li>
<li>more info!</li>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM