嵌套表格是禁止的吗?那怎么办呢?

时间:2014-07-21 06:21:23

标签: php html arrays forms checkbox

我的项目类似于这个例子。

<?php

if (isset($_POST['delete'])) {
  // Delete using id
}

if (isset($_POST['deleteAll'])) {
  // Delete using array name check
}

?>

<form method="POST" action="index.php" onsubmit="return validate()"> // First form
    <?php foreach ($rows as $row) : ?>
        <form method="POST" action="index.php" onsubmit="return validate()> // Second form
            <input type="checkbox" name="check[]" value="<?php $row['id'] ?>" />
            <input type="hidden" name="id" value="<?php $row['id'] ?>" />
            <input type="submit" name="delete" value="Delete" />
        </form>
    <?php endforeach ?>

    <input type="submit" name="deleteAll" value="Delete checked items" />
</form>

onsubmit过去曾使用javascript中的确认功能。

我已经考虑过这个问题,只能考虑使用nested-form

second formsubmit delete过去delete使用ID

first formsubmit deleteAll用于delete所有已检查的项目。

如果我delete second form

然后点击deleteid将成为last index的{​​{1}}。

所以我会foreach最后delete,而不是row我想row

我已经在思考,并没有发现任何线索,

我希望这里有人可以提供线索。

1 个答案:

答案 0 :(得分:6)

不要试图这样做。你只是让它变得更复杂。单一表格就足够了。

<form method="POST" action="index.php" onsubmit="return validate()">
  <?php foreach ($rows as $row) : ?>
      <input type="checkbox" name="check[]" value="<?php echo $row['id'] ?>" />
      <button name="delete" value="<?php echo $row['id']; ?>">Delete</button><br/>
  <?php endforeach ?>
  <input type="submit" name="deleteAll" value="Delete checked items" />
</form>

PHP

// handle single delete
if(isset($_POST['delete'])) {
  $id = $_POST['delete']; // individual ID
}

// handle mulitple selected checkboxes
if(isset($_POST['deleteAll'])) {
  $ids = $_POST['check'];
}