简体   繁体   English

如果XHTML不允许使用嵌套表单,那么如何/为什么要进行验证? 此代码可以使用吗?

[英]If XHTML does not allow nested forms, then how/why does this validate? Is this code ok to use?

I'm working on a webpage that lists several rental reservations from a database. 我正在处理一个网页,该网页列出了数据库中的一些租赁预订。 I think I may have built myself into a corner. 我想我可能已经将自己置入一个角落。 I originally had each row end with 3 cells, each one containing a tiny form made of 2 or 3 hidden fields and a button to "edit", "return", or "cancel" the reservation. 我最初每行的末尾都有3个单元格,每个单元格都包含一个由2或3个隐藏字段组成的小表格,以及一个用于“编辑”,“返回”或“取消”保留的按钮。

Those worked fine, and were all valid because each entire form was within a single cell. 这些工作正常,并且都是有效的,因为每个完整表格都在一个单元格内。

Now I need to add functionality to allow selecting multiple rows, and passing them to another page. 现在,我需要添加功能以允许选择多个行,并将它们传递到另一个页面。 So without thinking, I added a <form> that wrapped around the whole table, added a checkbox to each row, and a submit button at the bottom of the table. 因此,我不加思索地添加了一个<form> ,它包裹了整个表,在每行中添加了一个复选框,并在表底部添加了一个提交按钮。

After some research, I'm pretty sure nested forms are not allowed in XHTML. 经过研究,我很确定XHTML中不允许使用嵌套形式。 But the document still somehow validates with the W3C validator. 但是该文档仍然可以通过W3C验证器进行验证。 I'm using XHTML 1.0 Transitional. 我正在使用XHTML 1.0 Transitional。 Maybe because its not a <form> directly within another <form> , its a <form> within a <table> within a <form> ? 也许是因为它不是一个<form>直接在另一<form>它的一个<form>一个内<table>一个内<form> Is that allowed? 可以吗

The form works as it is (it submits a few extra fields from the top row, but I can just ignore those if I have to). 表单按原样工作(它从第一行提交了一些额外的字段,但是如果需要的话,我可以忽略它们)。

I'm just afraid to implement this because I feel like there's no way this is correct. 我只是害怕执行此操作,因为我觉得这是不可能的。 If anyone could clarify whether or not this is valid I would really appreciate it. 如果有人可以澄清这是否有效,我将不胜感激。 And if it's not, do you have any suggestions to fix it? 如果不是,您是否有任何解决建议?

Thanks in advance! 提前致谢!

<form id="form2" name="form2" method="post" action="pickup.php">
    <table width="1200" border="1" align="center" cellpadding="5" cellspacing="0" style="border-collapse:collapse; border-color:#CCC;">
        <tr>
            <td colspan="10" bgcolor="#CCCCCC"><h2>Equipment Being Picked Up Today</h2></td>
        </tr>
        <tr class="highlight">
            <td><input type="checkbox" name="res1" id="res1" value="2278" /></td>
            <td>George Washington</td>
            <td>555-333-4444</td>
            <td><a href="product.php?sku=R6209" target="_self">Lghting kit 6209</a></td>
            <td>2/12/15</td>
            <td>2/13/15</td>
            <td></td>
            <td align="center">
                <form name="editForm" method="post" action="edit.php">
                    <input name="editFrom" type="hidden" value="today2.php" />
                    <input name="theDate" type="hidden" value="2015-02-12" />
                    <input name="reservationID" type="hidden" value="2278" />
                    <input type="submit" name="edit" value="edit" />
                </form>
            </td>
            <td align="center">
                <form name="returnForm" method="post" action="today2.php">
                    <input name="reservationID" type="hidden" value="2278" />
                    <input name="return" type="hidden" value="yes" />
                    <input name="theDate" type="hidden" value="2015-02-12" />
                    <input type="submit" value="return" />
                </form>
            </td>
            <td align="center">
                <form name="cancelForm" method="post" action="cancel.php">
                    <input name="editFrom" type="hidden" value="today2.php" />
                    <input name="theDate" type="hidden" value="2015-02-12" />
                    <input name="reservationID" type="hidden" value="2278" />
                    <input type="submit" name="cancel" value="cancel" />
                </form>
            </td>
        </tr>
        <tr class="highlight">
            <td><input type="checkbox" name="res2" id="res2" value="2279" /></td>
            <td>Sam Adams</td>
            <td>333-222-7777</td>
            <td><a href="product.php?sku=R8300" target="_self">camera kit 3456</a></td>
            <td>2/12/15</td>
            <td>2/13/15</td>
            <td></td>
            <td align="center">
                <form name="editForm" method="post" action="edit.php">
                    <input name="editFrom" type="hidden" value="today2.php" />
                    <input name="theDate" type="hidden" value="2015-02-12" />
                    <input name="reservationID" type="hidden" value="2279" />
                    <input type="submit" name="edit" value="edit" />
                </form>
            </td>
            <td align="center">
                <form name="returnForm" method="post" action="today2.php">
                    <input name="reservationID" type="hidden" value="2279" />
                    <input name="return" type="hidden" value="yes" />
                    <input name="theDate" type="hidden" value="2015-02-12" />
                    <input type="submit" value="return" />
                </form>
            </td>
            <td align="center">
                <form name="cancelForm" method="post" action="cancel.php">
                    <input name="editFrom" type="hidden" value="today2.php" />
                    <input name="theDate" type="hidden" value="2015-02-12" />
                    <input name="reservationID" type="hidden" value="2279" />
                    <input type="submit" name="cancel" value="cancel" />
                </form>
            </td>
        </tr>
        <tr class="highlight">
            <td><input type="checkbox" name="res3" id="res3" value="2280" /></td>
            <td>Bob Dole</td>
            <td>111-222-4444</td>
            <td><a href="product.php?sku=R8609" target="_self">Other item 6789</a></td>
            <td>2/12/15</td>
            <td>2/13/15</td>
            <td></td>
            <td align="center">
                <form name="editForm" method="post" action="edit.php">
                    <input name="editFrom" type="hidden" value="today2.php" />
                    <input name="theDate" type="hidden" value="2015-02-12" />
                    <input name="reservationID" type="hidden" value="2280" />
                    <input type="submit" name="edit" value="edit" />
                </form>
            </td>
            <td align="center">
                <form name="returnForm" method="post" action="today2.php">
                    <input name="reservationID" type="hidden" value="2280" />
                    <input name="return" type="hidden" value="yes" />
                    <input name="theDate" type="hidden" value="2015-02-12" />
                    <input type="submit" value="return" />
                </form>
            </td>
            <td align="center">
                <form name="cancelForm" method="post" action="cancel.php">
                    <input name="editFrom" type="hidden" value="today2.php" />
                    <input name="theDate" type="hidden" value="2015-02-12" />
                    <input name="reservationID" type="hidden" value="2280" />
                    <input type="submit" name="cancel" value="cancel" />
                </form>
            </td>
        </tr>
        <tr>
            <td colspan="10">
                <input type="submit" name="submit" id="submit" value="Pickup Selected Equipment" />
            </td>
        </tr>
    </table>
</form>

It validates because the formal validation of XHTML 1.0 is based on XML rules, and XML is a strongly simplified modification of SGML, which is what HTML 4.01 is nominally based on. 之所以能够进行验证,是因为XHTML 1.0的正式验证是基于XML规则的,而XML是SGML的一种大大简化的修改,这是HTML 4.01名义上的基础。 Consequently, some features, such as nested forms, which are prohibited in all versions of HTML are not forbidden by the formal syntax of XHTML 1.0 described in a DTD based on XML. 因此,在基于XML的DTD中描述的XHTML 1.0正式语法并不禁止在所有HTML版本中禁止使用的某些功能(例如嵌套表单)。 The specification says this as follows: 规格说明如下:

SGML gives the writer of a DTD the ability to exclude specific elements from being contained within an element. SGML为DTD的编写者提供了将特定元素排除在元素内的能力。 Such prohibitions (called "exclusions") are not possible in XML. 这种禁止(称为“排除”)在XML中是不可能的。

For example, the HTML 4 Strict DTD forbids the nesting of an 'a' element within another 'a' element to any descendant depth. 例如,HTML 4 Strict DTD禁止将“ a”元素在另一个“ a”元素内嵌套到任何后代深度。 It is not possible to spell out such prohibitions in XML. 无法用XML阐明这种禁止。 Even though these prohibitions cannot be defined in the DTD, certain elements should not be nested. 即使不能在DTD中定义这些禁止,某些元素也不应嵌套。 A summary of such elements and the elements that should not be nested in them is found in the normative Element Prohibitions. 在规范性元素禁止中可以找到这些元素和不应嵌套在其中的元素的摘要。

And the Element Prohibitions says that a form element must not contain another form element. 元素禁止form元素不能包含另一个form元素。

It is not safe to nest forms. 嵌套表格并不安全。 There is no specification of what should happen if you do that. 没有说明如果执行此操作会发生什么情况。 For example, it is not specified whether the fields on an inner form should be included when an outer form is submitted. 例如,当提交外部表单时,不指定是否应包含内部表单中的字段。

Thus, you should consider restructuring the page so that form nesting is avoided. 因此,您应该考虑重组页面,从而避免表单嵌套。 If you need help with this, consider posting a new question that specifies the desired functionality and shows your best attempt at restructuring. 如果您需要帮助,请考虑发布一个新问题,该问题指定了所需的功能并显示了重组的最佳尝试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM