I have a form on my site where users can enter links to articles
So far... when a link is submitted, I am able to get that link to post to a destination html page.
However... if another link is submitted, it deletes the first one.
I would like the links to 'stack' and make a list to the destination (directory) page (which is currently an html page).
I don't know how to achieve this. Any advice or examples would be greatly appreciated.
I have include a very stripped down version of all three pages....
1.) The Form
<!DOCTYPE html>
<html>
<head>
<title>FORM</title>
<style>
body{margin-top:20px; margin-left:20px;}
.fieldHeader{font-family:Arial, Helvetica, sans-serif; font-size:12pt;}
.articleURL{margin-top:10px; width:700px; height:25px;}
.btnWrap{margin-top:20px;}
.postButton{cursor:pointer;}
</style>
</head>
<body>
<form action="urlUpload.php" method="post" enctype="multipart/form-data">
<div class="fieldHeader">Enter Article Link:</div>
<input class="articleURL" id="articleURL" name="articleURL" autocomplete="off">
<div class="btnWrap"><input class="postButton" type="submit" name="submit" value="POST"></button></div>
</form>
</body>
</html>
The Upload PHP (buffer) Page
<?php ob_start(); ?>
<!DOCTYPE html>
<html>
<head>
<title>urlUpload</title>
<style>body{margin-top:20px; margin-left:20px;}</style>
</head>
<body>
<?php $articleURL = htmlspecialchars($_POST['articleURL']); echo $articleURL;?>
</body>
</html>
<?php echo ''; file_put_contents("urlDirectory.html", ob_get_contents()); ?>
3.) The Destination HTML 'Directory List' page
<!DOCTYPE html>
<html>
<head>
<title>urlDirectory</title>
<style>body{margin-top:20px; margin-left:20px;}</style>
</head>
<body>
Sumbitted URL's should be listed here:
</body>
</html>
PS: I may not even need the middle php 'buffer' page. My knowledge of this sort of thing is limited thus far. If I don't need that, and can skip that page to accomplish my needs, please advise as well.
You can do this by using PHP to write the file and using urlDirectory.html
as a template. You will just need to change your php file:
urlUpload.php
<?php
function saveUrl($url, $template, $tag)
{
// If template is invalid, return
if (!file_exists($template)) {
return false;
}
// Remove whitespace from URL
$url = trim($url);
// Ignore invalid urls
if (!filter_var($url, FILTER_VALIDATE_URL)) {
return true;
}
// Read template into array
$html = file($template);
foreach ($html as &$line) {
// Look for the tag, we will add our new URL directly before this tag, use
// preg_match incase the tag is preceded or followed by some other text
if (preg_match("/(.*)?(" . preg_quote($tag, '/') . ")(.*)?/", $line, $matches)) {
// Create line for URL
$urlLine = '<p>' . htmlspecialchars($_POST['articleURL']) . '</p>' . PHP_EOL;
// Handle lines that just contain body and lines that have text before body
$line = $matches[1] == $tag ? $urlLine . $matches[1] : $matches[1] . $urlLine . $matches[2];
// If we have text after body add that too
if (isset($matches[3])) {
$line .= $matches[3];
}
// Don't process any more lines
break;
}
}
// Save file
return file_put_contents($template, implode('', $html));
}
$template = 'urlDirectory.html';
$result = saveUrl($_POST['articleURL'], $template, '</body>');
// Output to browser
echo $result ? file_get_contents($template) : 'Template error';