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>
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.
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).
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.
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.
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 td
s (td
s 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>
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>
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; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With