从iframe获取价值

时间:2009-10-14 06:54:39

标签: javascript iframe

我正在建立一个yellopages的网站,我想在其中上传并在某些页面上显示图像。

首先,我在上传时将图像名称插入数据库。我想在父页面上获取图像名称。

addproduct.php

<form action="" method="post" enctype="multipart/form-data" name="uploadform" id="uploadform">
  <table width="100%" border="0" cellspacing="0" cellpadding="0">
   <tr>
     <td width="42%" align="right">Product Title </td>
     <td width="1%">&nbsp;</td>
     <td width="118" height="118" align="center" bordercolor="#FFFFFF" bgcolor="#FFFFFF" ><iframe src="" id="fileframe" name="fileframe" width="118" height="118" scrolling="no" frameborder="0"></iframe></td>
   </tr>
   <tr>
     <td align="right">&nbsp;</td>
     <td>&nbsp;</td>
     <td onclick="whatever();">value</td>
   </tr>
   <tr>
     <td align="right">&nbsp;</td>
     <td>&nbsp;</td>
     <td><label>
      <input name="thefile" type="file" id="thefile" />
     <a href="#" class="uploadfile">upload</a></label></td>
   </tr>
   <tr>
     <td align="right">Product Description </td>
     <td>&nbsp;</td>
     <td>&nbsp;</td>
   </tr>
   <tr>
     <td align="right">&nbsp;</td>
     <td>&nbsp;</td>
     <td>&nbsp;</td>
   </tr>
   <tr>
     <td colspan="3"></td>
     </tr>
   <tr>
     <td>&nbsp;</td>
     <td>&nbsp;</td>
     <td><label>
       <input name="web" type="submit" id="web" value="Continue" />
     </label></td>
   </tr>
  </table>
</form> 

此页面中的Javascript

function whatever() {
   mmspobj=document.getElementId("fileframe");
   // if (mmspobj.tagName=='iframe'){
   //alert("hi");
   //mmsiobj=window.frames[fileframe].document.getElementId('fileframe').value;
   alert(mmspobj);
   //}
}

uploader.js

function init() {
   fileUploadEvents();
}

function fileUploadEvents() {
   var links = document.getElementsByTagName("a");

   if (links) {  
      for (var x=0; x<links.length; ++x) {
         if (links[x].className == "uploadfile")
         links[x].onclick = uploadFile;
      }
   }
}

function uploadFile() {
   var uploadForm = document.getElementById("uploadform");

   if (uploadForm) {
      uploadForm.target="fileframe";
      uploadForm.action="upload.php";
   }

   uploadForm.submit();
}

如何在addproduct.php中获取图片名称?

1 个答案:

答案 0 :(得分:0)

您可能会发现让iframe在父级中调用JavaScript函数更容易,而不是让父窗口尝试从iframe中读取名称。