Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

form within table-row tag

Tags:

html

Is it OK to write a form within the tr tag?

<table>
    % for my $word ( @$words_2 ) {
        <tr>
            <form action="/blacklist" method="post">
            <td><%=$word%></td>
            <td><input type="text" name="data" readonly hidden value="<%=$word%>" /></td>
            <td><input class="remove" type="submit" value="Remove" /></td>
            </form> 
        </tr>
    % }
</table>
like image 999
sid_com Avatar asked Oct 12 '11 08:10

sid_com


People also ask

Can we use form tag inside table tag?

It is valid to put a <form> tag inside a <table> tag. Editing note: tags wrapped as code so that the content reads as intended, but the answer should specify that while the form is inside the table, it cannot be a direct descendant: it must be a child of a cell.

Can a form be inside a table HTML?

You can have a form inside a table cell. You cannot have part of a table inside a form. Use one form around the entire table. Then either use the clicked submit button to determine which row to process (to be quick) or process every row (allowing bulk updates).

How do I add a form tag to a table?

Create an HTML table using the <table> element. Now add the <form> element within this table. Next, we will create form fields. We add the required form fields to the form using the <tr> element that is used to add rows to a table.

What is the tag for a row in a table?

The <tr> tag defines the table rows. There must be at least one row in the table. The <th> tag defines the header cells in the table which are displayed as bold, center-aligned text. The <td> tag defines the standard cells in the table which are displayed as normal-weight, left-aligned text.


3 Answers

The tr doesn't allow form-tags as direct children. Most modern browsers will let you do a lot of crap and so you could use this - but I wouldn't call it OK. A better approach would be to but the complete form into one of the tds (tds allow text, forms, inline- and block-elements as children):

<table>
    <% for my $word ( @$words_2 ) { %>
        <tr>
            <td><%=$word%></td>
            <td>
              <form action="/blacklist" method="post">
                <input type="text" name="data" readonly hidden value="<%=$word%>" />
                <input class="remove" type="submit" value="Remove" />
              </form> 
            </td>
        </tr>
    <% } %>
</table>

or, a lot easier, simply use a link (but note that data gets sent using GET instead of POST - maybe you'll have to change something in your code that handles the blacklisting):

<table>
    <% for my $word ( @$words_2 ) { %>
        <tr>
            <td><%=$word%></td>
            <td><a href="/blacklist?data=<%=$word%>">Remove</a></td>
        </tr>
    <% } %>
</table>
like image 168
oezi Avatar answered Sep 24 '22 12:09

oezi


Is it OK to write a form within the tr tag?

No. Forms can contain tables. Table cells can contain forms.

I'd approach this problem like so:

<form action="/blacklist" method="post">
    <fieldset>
    <legend>Remove</legend>
    % for my $word ( @$words_2 ) {
    <label>
        <input type="checkbox" name="data" value="<%=$word%>" />
        <%=$word%>
    </label>
    % }
    </fieldset>
    <input class="remove" type="submit" value="Remove" />
</form>
like image 29
Quentin Avatar answered Sep 26 '22 12:09

Quentin


No, that is not correct. The form tag has to be outside the table or inside a table cell.

Putting the form tag inside the table is an old trick to keep the form from taking up extra space. You should just use CSS for that:

form { margin: 0; padding: 0; }
like image 43
Guffa Avatar answered Sep 24 '22 12:09

Guffa