HTML - 嵌套表单解决方法? [英] HTML - Nested form work-around?

查看:130
本文介绍了HTML - 嵌套表单解决方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了向一个对PHP不太了解的人证明自己,我需要与一些由不应该被允许编程的人设计和构建的一些笨拙的黑客攻击系统一起工作。基本上,已经决定应允许用户在他们实际完成注册之前上载他们的个人资料的图像。就在那里,在注册表的中间,一个完全独立的表格(功能上,它必须看起来一样),一个文件上传字段,一个单独的提交按钮和一个图像显示区域。显然,该客户在另一个网站上看到了AJAX图像上传,并决定,这很酷,我们想要!没有真正知道表单如何工作或考虑到为什么一个未注册的用户应该上传东西开始



我最初的想法是在别处创建一个不可见的表单,然后复制单击按钮时的文件框值,然后是AJAX提交。这不起作用,因为JavaScript无法编辑文件框的值。可以理解。

第二次尝试是创建嵌套表单。通过一些黑客行为,我确实设法让FireFox看到两种形式,但提交内部提交外部提交。 未定义的行为,我相信是这个词。有意义的是,你不应该嵌套表单。

所以 - 忽略这个事实,这绝对没有意义,客户真的应该被告知 - 是还有什么办法可以在页面上创建一个文件上传表单,这个表单可以通过AJAX提交,并且明显位于另一个表单中?我希望避免只是在页面的其他地方创建表单,并用可恶的CSS黑客攻击它。

不知道这是否足够适合你,但是如何在表单中间嵌入 iframe 并在该窗口中进行文件上传呢?它可能不是纯粹的邪恶,真的是那么聪明和skullduggerous,但它可能工作。

唯一的副作用,我可以看到,如果当父提交,嵌入的 iframe 也是如此。如果你足够小心,并确保当它不用于文件上传时删除 iframe ,我认为你应该没问题。





 <!DOCTYPE html> 
< html>
< head>
< meta charset =ISO-8859-1>
< title>在此插入标题< / title>
< / head>
< body>
< form submit =http://www.google.com/searchmethod =get>
< div>
< input type =textname =qvalue =[Search Google Here]/>
< / div>
< iframe src =http://jfcoder.com/test/embed.phpstyle =width:200px; height:200px;>< / iframe>
< div>
< button type =submit>提交此页面的表单< / button>
< / div>
< / form>
< / body>
< / html>

嵌入

 <!DOCTYPE html> 
< html>
< head>
< meta charset =ISO-8859-1>
< title>在此插入标题< / title>
< / head>
< body>
<?php

if($ _GET ['q']){
echo< p>< strong> GET SUBMITTED< / strong>< p>中;
}

?>
< form submit =embed.phpmethod =get>
< div>
< input type =textname =qvalue =输入任何内容/>
< button type =submit>提交此页面的表单< / button>
< / div>
< / form>
< / body>
< / html>

http://jfcoder.com/test/parentform.php


In order to prove myself to someone who doesn't know much about PHP, I need to work with some awkwardly hacked-together system designed and built by people who probably shouldn't be allowed to program any more. Basically, it has been decided that users should be allowed to upload an image for their profile, before they actually finish registering. Just, right there, in the middle of the registration form, a completely separate form (functionally, it has to look the same) with a file upload field, a separate submit button, and an image display area. Apparently, the customer saw AJAX image uploading on another site, and decided, "That's cool, we want that!" without actually knowing how forms work or taking into account why an unregistered user should be uploading things to begin with

My initial idea was to create an invisible form elsewhere, and copy over the file box value when the button is clicked, followed by the AJAX submission. That doesn't work because JavaScript can't edit the value of a file box. Understandable.

Second attempt was to create nested forms. With some hacking, I did manage to get FireFox to see two forms, but submitting the inner one submits the outer one. "Undefined behavior", I believe is the term. Makes sense, you're not supposed to nest forms.

So - ignoring the fact that this makes absolutely no sense, and the client really ought to be told that - is there any way left to create a file upload form on a page, that can be submitted via AJAX, and is visibly located within another form? I'm hoping to avoid just creating the form elsewhere on the page and hammering it into place with abhorrent CSS hacks.

解决方案

I don't know if this is ingenious enough for you or not, but what about embedding an iframe in the middle of the form and doing the file upload within that window? It may not be pure evil enough to really be that clever and skullduggerous, but it may work.

The only side-effect I can see if that when the parent is submitted, so is the embedded iframe. If you're careful enough and make sure to remove the iframe when it's not in use for the file upload, I think you should be ok.

Parent

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form submit="http://www.google.com/search" method="get">
    <div>
        <input type="text" name="q" value="[ Search Google Here ]"/>
    </div>
    <iframe src="http://jfcoder.com/test/embed.php" style="width: 200px; height: 200px;"></iframe>
    <div>
        <button type="submit">Submit this Page's Form</button>
    </div>
</form>
</body>
</html>

Embedded

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<?php

if ($_GET['q']) {
    echo "<p><strong>GET SUBMITTED</strong></p>";
}

?>
<form submit="embed.php" method="get">
    <div>
        <input type="text" name="q" value="Type whatever"/>
        <button type="submit">Submit this Page's Form</button>
    </div>
</form>
</body>
</html>

http://jfcoder.com/test/parentform.php

这篇关于HTML - 嵌套表单解决方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆