1

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>
5
  • What is the value of $v1-3 Commented Feb 1, 2019 at 3:19
  • @khansen V1 is a url, V2 is description text string, V3 is a date in the format of January 1st 2019 Commented Feb 1, 2019 at 3:25
  • 1
    Then all you have to do is add the html tags to $content Commented Feb 1, 2019 at 3:28
  • Sorry if I'm missing the obvious , but when I try something like : $content = <li><a href=$v1. PHP_EOL .$v2. PHP_EOL .$v3; It doesn't seem to like it Commented Feb 1, 2019 at 3:35
  • @Harvtronix answer explains exactly what you have to do. Edit: There is missing a . in front of $v1 Commented Feb 1, 2019 at 3:37

1 Answer 1

1

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>
Sign up to request clarification or add additional context in comments.

2 Comments

@KHansen Thanks for keeping me honest! I originally called that var $info, but then switched to the var names used by the OP and only got 2/3rds of the way there.
could you possibly look at stackoverflow.com/questions/54473156/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.