【问题标题】:Making An HTML Directory List制作 HTML 目录列表
【发布时间】:2017-06-17 02:07:02
【问题描述】:

我的网站上有一个表单,用户可以在其中输入文章的链接

到目前为止...提交链接后,我可以将该链接发布到目标 html 页面。

但是...如果提交了另一个链接,它会删除第一个链接。

我想要指向“堆栈”的链接并列出目标(目录)页面(当前是一个 html 页面)。

我不知道如何实现这一点。任何建议或示例将不胜感激。

我已经包含了所有三个页面的非常精简的版本....

1.) 形式

    <!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>
  1. 上传 PHP(缓冲区)页面

    <?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.) 目标 HTML“目录列表”页面

    <!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:我什至可能不需要中间的 php 'buffer' 页面。到目前为止,我对这类事情的了解是有限的。如果我不需要,并且可以跳过该页面来满足我的需求,也请告知。

【问题讨论】:

  • 您是将这些值存储在数据库中还是只是想添加到 html 文件中的列表中?
  • 没有 sjdaws。不存储在数据库中。只想使用表单提交在html文件中生成列表。

标签: php database forms list html-lists


【解决方案1】:

您可以通过使用 PHP 编写文件并使用 urlDirectory.html 作为模板来做到这一点。您只需要更改您的 php 文件:

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';

【讨论】:

  • 谢谢你。我对那个新的 php 应该去哪里有点困惑?我从问题中的 3 个差异页面开始。在你的例子中......你的例子有多少?
  • 抱歉,您的表格很好,我的回答分别替换了urlUpload.phpurlDirectory.html
  • 好吧,我想我只是感到困惑,因为 urlUpload 文件看起来与现在的更改相同。
  • 是的,唯一的区别是我删除了占位符。
  • 好的。手指交叉,要开枪了。我会回来报告的。谢谢。
猜你喜欢
  • 2012-11-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-14
  • 1970-01-01
  • 1970-01-01
  • 2012-02-28
  • 2016-11-10
相关资源
最近更新 更多