echo -> html = chaos


echo -> html = chaos


我试图在HTML表中用PHP输出一个数据库表。当我在HTML中查看输出时,它完全是一团糟。在我看来,一切都是正确的。也许你们中的某个人看到了错误。

我的PHP文件:

<table id="table_id" class="display table table-hover table-responsive table-striped">
<thead>
<tr>
<th class="col-md-3">Gespeichert am</th>
<th class="col-md-5">Text</th>
<th class="col-md-3">Kategorie</th>
<th class="col-md-1"><span class='glyphicon glyphicon-remove' aria-hidden='true'></span></th>
</tr>
</thead>
<tbody>
<?php
while($row=mysql_fetch_object($ergebnis))
{
echo "<tr>";
echo "<td>".date("H:i - d.m.y",strtotime($row->Zeit) + 60*60)."</td>";
echo "<td><a href='".$row->url."' target='_blank'>".$row->text."</a></td>";
echo "<td>".$row->kategorie."</td>";
echo "<form action='delete.php' method='post'>";
echo "<td>";
echo "<input type='hidden' name='delete_id' value='".$row->id."'</td>";
echo "<button onClick='submit();' style='border:none; background-color:transparent; padding:0px;' type='submit' value='Delete'><span class='glyphicon glyphicon-remove delete' aria-hidden='true'></span></button>";
echo "</td>";
echo "</form>";
echo "</tr>";
}
?>
</tbody>
</table>



我的HTML输出如下所示:

<table id="table_id" class="display table table-hover table-responsive table-striped">
<thead>
<tr>
<th class="col-md-3">Gespeichert am</th>
<th class="col-md-5">Text</th>
<th class="col-md-3">Kategorie</th>
<th class="col-md-1"><span class="glyphicon glyphicon-remove" aria-hidden="true"></span></th>
</tr>
</thead>
<tbody>
<tr>
<td>15:46 - 26.03.15</td>
<td><a href="http://Www.google.de" target="_blank">Google link</a></td>
<td>Web</td>
<form action="delete.php" method="post"></form>
<td><input type="hidden" name="delete_id" value="37" <="" td="">
<button onclick="submit();" style="border:none; background-color:transparent; padding:0px;" type="submit" value="Delete">
<span class="glyphicon glyphicon-remove delete" aria-hidden="true"></span>
</button>
</td>
</tr>
</tbody>
</table>

<form>放在表的单元格内(在<td>内),或将其包裹在整个表中。

问题是<tr>只允许<th><td>节点在里面,这就是为什么<form>变得一团糟的原因。

此外,正如有人在评论中提到的那样,下面一行有错误,导致html:损坏

echo "<input type='hidden' name='delete_id' value='".$row->id."'</td>";

应该是:

echo "<input type='hidden' name='delete_id' value='".$row->id."'></td>";