使用引导程序模式,我创建了一个Web表单,该表单收集有关员工工作时间的一些信息,需要将其插入数据库中。当不在模态中时,代码运行良好,但是一旦我将代码插入模态中时,单击提交时,模态就会关闭,并且表单中的内容永远不会进入数据库。有什么我想念的吗?
<!-- Modal -->
<div class="modal fade" id="submit-time" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Submit Hours for <?php echo htmlspecialchars($_SESSION["username"]); ?></h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<?php
/**
* Use an HTML form to create a new entry in the
* timeclock table.
*
*/
if (isset($_POST['submit'])) {
require "config.php";
require "common.php";
try {
$connection = new PDO($dsn, $username, $password, $options);
$new_user = array(
"employee_id" => $_POST['employee_id'],
"date" => $_POST['date'],
"hours" => $_POST['hours'],
"comments" => $_POST['comments'],
"activity_id" => $_POST['activity_id']
);
$sql = sprintf("INSERT INTO %s (%s) values (%s)",
"timesheet",
implode(", ", array_keys($new_user)),":" . implode(", :", array_keys($new_user)));
$statement = $connection->prepare($sql);
$statement->execute($new_user);
} catch(PDOException $error) {
echo $sql . "<br>" . $error->getMessage();
}
}
?>
<?php if (isset($_POST['submit']) && $statement) { ?>
<blockquote><?php echo $_POST['employee_id']; ?> successfully added.</blockquote>
<?php } ?>
<h2>Submit Hours for <?php echo htmlspecialchars($_SESSION["username"]); ?> </h2>
<form method="post" enctype="multipart/form-data">
<label for="employee_id">employee_id</label>
<input type="text" name="employee_id" id="employee_id" value="<?php echo htmlspecialchars($_SESSION["id"]); ?>" readonly><br>
<label for="date">date</label>
<input type="text" name="date" id="date"><br>
<label for="hours">hours</label>
<input type="text" name="hours" id="hours"><br>
<label for="comments">comments</label>
<input type="text" name="comments" id="comments"><br>
<label for="activity_id">activity_id</label>
<select id="activity_id" name="activity_id"> <br>
<option value="1">Shop Hours</option>
<option value="2">Show Hours</option>
</select>
<input type="submit" name="submit" class="btn btn-default" value="submit">
</form>
<?php require "templates/footer.php"; ?>
</div>