php 通过 post [文件上传] 将表单数据提交到 iframe

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1568412/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:06:35  来源:igfitidea点击:

submit form data into iframe via post [file upload]

phpformsiframepost

提问by stormdrain

UGH.

啊。

Hi.

你好。

I have a form. I'd like to know how/if I could submit this form to an iFrame that has the page that will handle the file upload/naming.

我有一个表格。我想知道如何/是否可以将此表单提交到具有处理文件上传/命名页面的 iFrame。

If I try even something simple like post an input/text to the form, nothing happens (the handler is set to echo the $_POST). I have tried setting the iframe name/id et. al. and setting the form target to the respective iframe name/id. When I hit submit, the iframe just sits there like a dummy. WTF am I doing wrong?

如果我尝试一些简单的事情,例如将输入/文本发布到表单,则不会发生任何事情(处理程序设置为回显 $_POST)。我试过设置 iframe 名称/id 等。阿尔。并将表单目标设置为相应的 iframe 名称/ID。当我点击提交时,iframe 就像一个假人一样坐在那里。WTF我做错了吗?

Thx.

谢谢。

<form action="/clients/testAddTrans/<?=$clientID?>" id="reportEdit" class="EditName" method="POST" target="transFrame">
    <div class="inputDiv">
        <span class="inputLabel">Description:</span>
        <span class="textInput">
            <input type="text" id="transDesc" name="transDesc" value="" size="40" class=""/>
        </span>
    </div>
    <div class="inputDiv">
        <span class="inputLabel">Date:</span>
        <span class="textInput">
            <input type="text" id="date" name="transDate" value="" size="40" class=""/>
        </span>
    </div>
    <div class="inputDiv">
        <span class="inputLabel">File:</span>
        <span class="textInput">
            <input type="file" id="file" name="transFile" value="" size="40" class=""/>
        </span>
    </div>
    <input name="name_id" type="hidden" value="<?=$itemid?>" />
    <input type="submit" value="Submit" name="submit"/>
    <input type="button" class="secondaryAction" onclick="hideOverDiv()" value="Close"/>
    <div id="overDivNotice" class="overDivNotice" style="display:none"></div>
    <iframe action="/clients/testAddTrans/<?=$clid?>" id="transFrame" name="transFrame" style=""></iframe>
</form>

Generated html via firebug:

通过 firebug 生成的 html:

    <div class="content" id="overDivContent"><div class="inputDivContainer">
  <fieldset class="inputOverDiv" id="tfa_Names">
 <legend><b>Add Transmittal:</b></legend>
  <div class="data"><form target="transFrame" method="POST" class="EditName" id="reportEdit" action="/clients/testAddTrans/fsdf1556"><div class="inputDiv"><span class="inputLabel">Description:</span><span class="textInput"><input type="text" class="" size="40" value="" name="transDesc" id="transDesc"/></span></div><div class="inputDiv"><span class="inputLabel">Date:</span><span class="textInput"><input type="text" class="" size="40" value="" name="transDate" id="date"/></span></div><div class="inputDiv"><span class="inputLabel">File:</span><span class="textInput"><input type="file" class="" size="40" value="" name="transFile" id="file"/></span></div><input type="hidden" value="121" name="name_id"/>
  </form><br/>
  <div align="center" class="actions" id="overDivActions">
   <input type="submit" name="submit" value="Submit"/>
   <input type="button" value="Close" onclick="hideOverDiv()" class="secondaryAction"/>
  </div>
  <div style="display: none;" class="overDivNotice" id="overDivNotice">
  </div></div>


  <iframe style="" name="transFrame" id="transFrame">tyh</iframe>
  </fieldset>
  </div></div>

I don't know why it is putting the </form>tag where it is.. It is supposed to be after the iframe, but whatever. Does that even matter? Is the iframe supposed to be inside the form?

我不知道为什么它把</form>标签放在它所在的地方......它应该在 iframe 之后,但无论如何。这还重要吗?iframe 应该在表单内吗?

回答by Cesar

Nice, I was wrong.. I found the problem. First use html for write html; With the code below works:

很好,我错了。我发现了问题。先用html写html;使用下面的代码工作:

for 'testSubmitiFrame.html':

对于“testSubmitiFrame.html”:

    <form target="transFrame" method="POST" class="EditName" id="reportEdit" action="testSubmitiFrame.php">
<div class="content" id="overDivContent">
    <div class="inputDivContainer">
        <fieldset class="inputOverDiv" id="tfa_Names">
        <legend><b>Add Transmittal:</b></legend>
        <div class="data">
            <div class="inputDiv">
                <span class="inputLabel">Description:</span>
                <span class="textInput"><input type="text" class="" size="40" value="" name="transDesc" id="transDesc"/></span>
            </div>
            <div class="inputDiv">
                <span class="inputLabel">Date:</span>
                <span class="textInput"><input type="text" class="" size="40" value="" name="transDate" id="date"/></span>
            </div>
            <div class="inputDiv">
                <span class="inputLabel">File:</span>
                <span class="textInput"><input type="file" class="" size="40" value="" name="transFile" id="file"/></span>
            </div>
            <input type="hidden" value="121" name="name_id"/>
            <br/>
            <div align="center" class="actions" id="overDivActions">
                <input type="submit" name="submit" value="Submit"/>
                <input type="button" value="Close" onclick="hideOverDiv()" class="secondaryAction"/>
            </div>
            <div style="display: none;" class="overDivNotice" id="overDivNotice"></div>
        </div>
        </fieldset>
    </div>
</div>
</form>
<iframe style="" name="transFrame" id="transFrame">tyh</iframe>

for 'testSubmitiFrame.php':

对于“testSubmitiFrame.php”:

<?php
var_dump($_POST);
?>

Your problem is html syntax. This works.

你的问题是 html 语法。这有效。

回答by stormdrain

Apparently, a div or stupid fieldset tag being out of place was preventing the thing from working. I really gotta start checking my code before bothering you nice people.

显然,一个不合适的 div 或愚蠢的 fieldset 标签阻止了它的工作。在打扰你们这些好人之前,我真的得开始检查我的代码了。

Thanks anyway.

不管怎么说,还是要谢谢你。

回答by Greg

I think it's because of these two lines:

我认为这是因为这两行:

echo "</div>";
echo "</div><br>

You're closing <div>with no opening tags. This is making Firefox close the <form>early - your submit button isn't inside your form, which is why it's not working.

你关闭<div>没有开始标签。这使 Firefox<form>提前关闭- 您的提交按钮不在表单内,这就是它不起作用的原因。

The position of the iframe inside or outside the form doesn't matter - just make sure the rest of the HTML is valid and it should work.

iframe 在表单内部或外部的位置无关紧要 - 只需确保 HTML 的其余部分有效并且它应该可以工作。

回答by R0b0tn1k

Your logic appears valid, can you setup a test page?

您的逻辑似乎有效,您可以设置测试页面吗?

回答by James Black

This page will do what you want, so you may want to look through it and see what may be different from what you are already doing: http://www.anyexample.com/programming/php/php_ajax_example__asynchronous_file_upload.xml

此页面将执行您想要的操作,因此您可能想浏览它并查看与您已经在做的事情可能不同的地方:http: //www.anyexample.com/programming/php/php_ajax_example__asynchronous_file_upload.xml

Is the action in the html the php file that will do the processing?

html 中的操作是将进行处理的 php 文件吗?