dpn4073 2017-06-17 02:07
浏览 91
已采纳

制作HTML目录列表

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>
  1. 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.

  • 写回答

1条回答 默认 最新

  • donglugou6652 2017-06-17 02:39
    关注

    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';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!