【问题标题】:Form not passing along iframe input表单不通过 iframe 输入
【发布时间】:2017-10-01 17:21:35
【问题描述】:

我最近一直在开发一个具有表单的网络应用程序。但是,我遇到了障碍。在此表单中,有一个上传照片的iframe。提交后,照片会上传并让用户知道。此页面上的表单中还有一个隐藏的输入。在这个隐藏的输入中,存储了图像的 URL。这是我的脚本,应该拦截此输入的值并以以下形式传递:

window.onload = function() {
    document.getElementById("photoFrame").contentDocument.forms[0].onsubmit = function () {
        document.getElementById("entry_862192515").setAttribute("value", document.getElementById("photoFrame").contentDocument.forms[0].getElementById("photoUrl").value);
    }
}

这是我的 HTML:

<iframe width="100%" name="select_frame" src="/photo-upload.php" allowtransparency="true" frameborder="0" id="photoFrame"></iframe>

photo-upload.php 只是一个简单的上传图片的表单。现在这里是upload.php,从 POST 请求上传图像的 PHP 脚本:

<?php

// Cloudinary init
require 'Cloudinary.php';
require 'Uploader.php';
require 'Api.php';
\Cloudinary::config(array( 
  "cloud_name" => "(my name)", 
  "api_key" => "(my key)", 
  "api_secret" => "(my secret)"
));

// Uploads images to Cloudinary
$uploaded_file = \Cloudinary\Uploader::upload($_FILES["fileToUpload"]["tmp_name"]);

// Declares URL to variable
$url = $uploaded_file['secure_url'];

echo '<form><input type="hidden" id="photoUrl" value="'.$url.'" /></form>';
echo '<b style="color:green;">Success! Photo uploaded. Once you finish filling out the form, click the "Send" button.</b>';

?>

这个脚本有什么问题?运行控制台时,我在控制台中看不到任何错误,在 iframe 中,输入包含 URL 值。我究竟做错了什么?非常感谢。

【问题讨论】:

  • 不看你的html很难说是什么问题
  • 完成!我现在已经发布了代码。

标签: javascript html forms iframe


【解决方案1】:
<form>
  Hidden parent's input for photo URL:
  <input id="saveURL" />
  <iframe name="select_frame" src="iFrame.html" id="photoFrame"></iframe>
</form>

<script>

  // wait till parent loads so we don't trigger it during iFrame's first load
  window.onload = function() {

    var parentFormInput = document.getElementById('saveURL');
    var photoFrame = document.getElementById("photoFrame");

    // executes after the iFrame postback completes
    photoFrame.onload = function() {
      var innerDoc = this.contentDocument || this.contentWindow.document;
      var url = innerDoc.getElementById('photoUrl').value; // iframe input
      parentFormInput.value = url; // saves value to the iframe's parent
    }
  }

</script>

iFrame.html 包含表单:

<form>
  <input id="photoUrl" style="width:60%" readonly="readonly" value="http://somedomain.com/somefile.jpg" />
</form>

演示:http://plnkr.co/edit/auqk8tQrCYYEIFjJUmPC?p=preview

【讨论】:

  • 如果照片是必填项,您可以在主表单的提交事件中验证saveURL 输入字段不为空
  • var parentFormInput = document.getElementById('entry_862192515'); var photoFrame = document.getElementById("photoFrame"); photoFrame.contentWindow.onhashchange = function() { setTimeout(function () { var innerDoc = this.contentDocument || this.contentWindow.document; var url = innerDoc.getElementById('photoUrl').value; // iframe input parentFormInput.value = url; // saves value to the iframe's parent }, 500); }
  • 由于某种原因,我仍然收到此错误:global code — report:53TypeError: null is not an object (evaluating 'photoFrame.contentWindow')
  • 另外,有没有iframe-less 的方式来完成这个?
  • 我不明白您为什么要添加photoFrame.contentWindow.onhashchange()setTimeout?我为您提供了检测 iframe 何时刷新的工作代码。此外,该错误的可能原因是浏览器不支持contentWindow。这就是使用 this.contentDocument || this.contentWindow.document 行的原因 - 以支持不同的浏览器
【解决方案2】:

photo-upload.php 文件需要发布一个文件数据,以便可以从 $_FILES 中检索该文件数据。但是当 iframe 被加载时,文件数据怎么会被发布呢?我猜它会在php中抛出错误。

【讨论】:

  • 你还有什么其他的方法思路吗?
  • 如果不完全了解您在这里想要达到的目标,我无法帮助您。就像在这种情况下为什么要使用 iframe 一样,照片上传是否将由用户等发起。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-25
  • 1970-01-01
  • 2020-04-13
  • 2017-12-22
  • 2016-03-01
  • 1970-01-01
相关资源
最近更新 更多