获取复选框的值提交给变量

时间:2014-03-25 03:12:21

标签: php checkbox

我正在尝试弄清楚如何获取复选框的值将其提交给一个来自sql db的id的变量。

以下是我目前的代码:

- Index.php

<?php

    $sql = "SELECT * FROM `file`";

      if (!$result = $db->query($sql)){
          die('There was an error running the query [' . $db->error . ']');
          }
    ?>

    <form method="post">

    <a href="includes/AED/file_add.php" data-toggle="modal" data-target="#NEW"><button class="btn btn-success">New</button></a>
    <a href="includes/AED/file_edit.php" data-toggle="modal" data-target="#EDIT"><button type="submit" class="btn btn-warning">Edit</button></a>
    <a href="includes/AED/file_delete.php" data-toggle="modal" data-target="#DELETE"><button class="btn btn-danger">Delete</button></a>

    <table class="table table-condensed">

      <thead>
        <th>#</th>
        <th>Name</th>
        <th>Category</th>
        <th>Date</th>
      </thead>

      <tbody>

        <?php  while($row = $result->fetch_assoc()){ ?>

        <td><input type="checkbox" name="id_grab" value="<?php echo $row['file_id']; ?>"></td>
        <td><a href="simple/files/<?php echo $row['file_location']; ?>"><?php echo $row['file_name']; ?></a></td>
        <td><?php echo $row['file_category']; ?></td>
        <td><?php echo date('d-m-y', strtotime($row['file_date'])); ?></td>

      </tbody>

    <?php } ?>
    </table>

    </form>

- file_edit.php

<?php include('../db_connect.php') ;

$id=isset($_POST['id_grab']);

    $sql = "SELECT * FROM `file` WHERE file_id = $id";

      if (!$result = $db->query($sql)){
          die('There was an error running the query [' . $db->error . ']');
          }

        $row = $result->fetch_assoc();
?>



<div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;       </button>
    <h4 class="modal-title" id="myModalLabel">Edit Invoice / Receipt</h4>
  </div>
  <div class="modal-body">

    <form action="../includes/file_update.php" method="post" role="form" enctype="multipart/form-data">

        <div class="form-group">
            <label for="File_Name">Name</label>
            <input type="text" name="file_name" class="form-control" id="file_name" value="<?php echo $row['file_name']; ?>">
        </div>

        <div class="form-group">
            <lable for="File_Category">Category</label>
            <input type="text" name="file_category" class="form-control" id="file_category" placeholder="Financial">
        </div>

        <div class="form-group">
            <label for="file_tag">Tag</label>
            <input type="text" name="file_tag" class="form-control" id="file_tag" placeholder="statement">
        </div>

        <div class="form-group">
            <label for="file_description">Description</label>
            <input type="text" name="file_description" class="form-control" id="file_description">
        </div>

        <div class="form-group">
            <label for="file_date">Date</label>
            <input type="date" name="file_date" class="form-control" id="file_date">
        </div>

        <div class="form-group">
            <label for="file_location">File</label>
            <input type="file" class="form-control" name="file_location" value="file">
            <p class="help-block">Select appropriate file for upload.</p>
        </div>

    <button type="button" class="btn btn-info" data-dismiss="modal">Cancel</button>
    <button type="submit" class="btn btn-success">Upload</button>


    </form>
</div>

我无法解决这个问题,花了好几个小时在网上看...我关心的是使用Bootstraps Modal我不确定它是否能够获得POST数据...好像我输入了ID手动工作......

非常感谢任何协助。

1 个答案:

答案 0 :(得分:0)

更改此

$id=isset($_POST['id_grab']);

$id=isset($_POST['id_grab'])?$_POST['id_grab']:'';
//if id_grab dont have a value then $id value will be ''

isset()如果var存在且返回TRUE,则返回NULL以外的值,否则为FALSE