我正在创建一个客户端列表及其详细信息,这些列表是从数据库导入的。在每一行的末尾,都有一个“编辑”按钮,它将在一个表格中显示有关该客户的所有信息,这些信息将被编辑。
当从数据库导入客户端列表时,我已经用PHP编写了表单 -
while($row = mysqli_fetch_array($result))
{
echo" <tr id=".$count.">";
echo" <form method='post' id='edit_form' action='search_client_details.php?edit=true'>";
echo" <td><input type='checkbox' id='chkStatus' name='customer' class='chkbox' value='".$row['client_id']."'></td>";
echo" <td>".$row['client_name']."</td>";
echo" <td>".$row['client_address']."</td>";
echo" <td>".$row['client_type']."</td>";
echo" <td><input type='submit' name='submit' id='edit_form_submit' value='Edit' class='btn btn-success' style='background-color:#27ae60; color:#fff'></td>";
echo" </form>";
echo" </tr>";
$count++;
}
正确打印列表。但是当按下编辑按钮时,它不起作用。检查行上的元素显示了这个 -
<tr id="1" style="display: table-row;">
<form method="post" id="edit_form" action="search_client_details.php?edit=true"></form>
<td><input type="checkbox" id="chkStatus" name="customer" class="chkbox" value="1"></td>
<td>Name</td>
<td>address</td>
<td>type</td>
<td><input type="submit" name="submit" id="edit_form_submit" value="Edit" class="btn btn-success" style="background-color:#27ae60; color:#fff"></td>
如您所见,首先打印表单标签,然后打印其余部分。为什么会这样?我是初学者,所有的帮助表示赞赏。
答案 0 :(得分:2)
之所以发生这种情况是因为您在打开表单标记后立即创建了<td>
元素。您的HTML标记无效。请考虑以下两行:
echo "<form method='post' id='edit_form' action='search_client_details.php?edit=true'>";
echo"<td><input type='checkbox' id='chkStatus' name='customer' class='chkbox' value='".$row['client_id']."'></td>";
您会看到第二行有另一个开头td
元素,在这种情况下无效
<强>加强>
如果您想要提交整个内容,则必须使用数组作为元素的名称。将表单的标记放在表格之外,并在表格的结束标记后关闭表单标记。
<form method='post' id='edit_form' action='search_client_details.php?edit=true'>
<table>
.... any markup
<?php
while($row = mysqli_fetch_array($result))
{
echo" <tr id=".$count.">";
echo" <td><input type='checkbox' id='chkStatus' name='customer[]' class='chkbox' value='".$row['client_id']."'></td>";
echo" <td>".$row['client_name']."</td>";
echo" <td>".$row['client_address']."</td>";
echo" <td>".$row['client_type']."</td>";
echo" </tr>";
$count++;
}?>
</table>
<input type='submit' name='submit' id='edit_form_submit' value='Edit' class='btn btn-success' style='background-color:#27ae60; color:#fff'>
</form>
然后在服务器端search_client_details.php
中,您可以使用for
循环来迭代所有代码:
for( $i = 0,$length = count($_POST['customer']); $i < $length; $i++ ) {
echo $_POST['customer'][$i]; // this will show all the data sending from form
// do the operation here
}
答案 1 :(得分:1)
您无法在tr属性中添加表单。而不是它你可以在现有的内容中关注新表并将其分配给表单
试试这个:
while($row = mysqli_fetch_array($result))
{
echo " <tr id=".$count.">";
echo " <td><form method='post' id='edit_form' action='search_client_details.php?edit=true'><table><tr>";
echo " <td><input type='checkbox' id='chkStatus' name='customer' class='chkbox' value='".$row['client_id']."'></td>";
echo " <td>".$row['client_name']."</td>";
echo " <td>".$row['client_address']."</td>";
echo " <td>".$row['client_type']."</td>";
echo " <td><input type='submit' name='submit' id='edit_form_submit' value='Edit' class='btn btn-success' style='background-color:#27ae60; color:#fff'></td>";
echo "</tr></table></form></td>";
echo "</tr>";
$count++;
}