使用Bootstrap Modal表单添加ADD无效

时间:2016-04-06 13:07:54

标签: javascript php jquery html bootstrap-modal

我想使用模态表单来填充当前表格,但它不会起作用。我只是需要一些帮助,我可能在这里错过了一些东西。谢谢!

这是我的按钮代码:

<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#addModal" href="add_dept.php">+ Add Department</button>

这是我的文件底部的代码,用于显示模态内容:

<div class="modal fade" id="addModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
    <div class="modal-content">
    </div>
</div>

这是我的add_dept.php代码:

<?php include("../includes/config.php");
$reqErr = "";
if($_SERVER['REQUEST_METHOD'] == "POST") {
  if(!empty($_POST['dept_name']) && !empty($_POST['dept_code'])) {
    $dept_name = $_POST['dept_name'];
    $dept_code = $_POST['dept_code'];
    $query_addDept = "INSERT INTO department(dept_name,dept_code) VALUES('$dept_name','$dept_code')";
    if(mysqli_query($con,$query_addDept)) {
        $reqErr = '<div class="alert alert-success" id="myAlert">
              <a href="" class="close"> &nbsp;  <i class="fa fa-times"></i></a>
            <i class="fa fa-check-circle"></i> Department added</div>';
            mysqli_close($con);
    }
    else {
      $reqErr = '<div class="alert alert-danger" id="myAlert">
            <a href="" class="close"> &nbsp;  <i class="fa fa-times"></i></a>
          <i class="fa fa-exclamation-circle"></i> Failed to add Department</div>';
    }
  }
}
?>

<?php echo $reqErr; ?>
<div class="modal-header">
    <button type="button" class="close" data-dismiss="modal">&times;</button>
    <h4 class="modal-title">Add Department</h4>
</div>
<div class="modal-body">
  <form method="POST" class="form" id="add-department" role="form">
  <div class="form-group">
    <input type="text" class="form-control" id="dept_name" name="dept_name" placeholder="Department Name" />
    <span class="help-block"></span>
  </div>
  <div class="form-group">
    <input type="text" class="form-control" id="dept_code" name="dept_code" placeholder="Department Code" />
    <span class="help-block"></span>
  </div>
  <div class="modal-footer">
    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
    <input type="submit" id="submit_btn" class="btn btn-primary" />
  </div>
  </form>
</div>

2 个答案:

答案 0 :(得分:1)

data-target="#addModal" href="add_dept.php"

你的问题就在这里。

您无法将按钮设置为模型触发器和创建页面的链接。

你可以触发模态,然后点击模态,重定向到你的add_dept.php页面,或者只是点击重定向,而无需使用模态。

答案 1 :(得分:1)

  <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#addModal" href="add_dept.php">+ Add Department</button>

删除href =&#34;&#34;

 <div class="modal fade" id="addModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
     <div class="modal-dialog" role="document">
         <div class="modal-content">
              <?php require_once('add_dept.php'); ?>
         </div>
     </div>
</div>

添加最后一个 - 关闭div