【问题标题】:Save DOM Element using PHP使用 PHP 保存 DOM 元素
【发布时间】:2012-12-12 20:44:57
【问题描述】:

我的网页中有这个视频元素:

    <video height="240" width="412" id="video" controls="controls">
    </video>
<button type="button"  id ="url_go" name="url_go" onclick="showUploadDiv()"> Load </button>
<form id="upload" action="save.php" method="post" enctype="multipart/form-data">
<button type="submit" > Save </button>

它有一个以这种方式加载的动态url:

function showUploadDiv()
{
    if ($("#url").val()==null || $("#url").val()=="")
        return false; 

    var video = document.getElementById('video');
    video.src = $("#url").val();
    video.controls = true;
    video.height = 240; 
    video.width = 412;
    document.getElementById('url').value="";
}

我使用按钮load 在我的网页中上传视频(设置值 url)。之后我想保存我的元素video。 我的问题出在文件save.php 中,我想在其中保存加载了新 URL 的视频元素。在此代码中,我将网页元素保存在新网页中,但视频未加载 url 值:

<?php
    $ourFileName = "testFile.html";
    $ourFileHandle = fopen($ourFileName, 'w+') or die("can't open file");
    fwrite($ourFileHandle, file_get_contents("index.html"));
    fclose($ourFileHandle);
?>

有人有解决办法吗?

【问题讨论】:

  • 不确定我是否明白这一点?您是说用户输入视频链接,然后您会在您的网站上播放该视频,并且您还想将实际视频保存在服务器上?
  • 您只是在用户浏览器中更改页面的内容。发生这种情况时,页面不会保存到服务器。

标签: php javascript html load save


【解决方案1】:

当您使用 JavaScript 更改用户浏览器中的 DOM 时,服务器上的原始 HTML 文件实际上并没有更改。您需要上传 URL,并在保存之前手动将“src”属性插入到加载的 HTML 代码中。

<?php
    $ourFileName = "testFile.html";
    $ourFileHandle = fopen($ourFileName, 'w+') or die("can't open file");
    $url = $_POST['url']; // don't forget to validate this
    $content = file_get_contents("index.html");
    $content = str_replace('<video ','<video src="'.$url.'" ',$content);
    fwrite($ourFileHandle, $content);
    fclose($ourFileHandle);
?>

【讨论】:

    猜你喜欢
    • 2011-09-22
    • 2016-09-05
    • 2021-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-26
    • 2012-03-06
    • 1970-01-01
    相关资源
    最近更新 更多