[英]Working with edit form inside bootstrap modal dialog using php
所以基本上我有一張員工基本信息表。 它已經具有從mysql數據庫中檢索的值。 員工基本信息表 。 正如您在桌面上看到的那樣,它右上角有一個編輯按鈕。 單擊后,將顯示模態對話框。 編輯基本信息模態 。
我的問題是,一旦用戶填寫了編輯表單上的字段並單擊了保存按鈕,我就不知道如何更新基本信息表上的記錄。
我想要發生的是當用戶點擊模態對話框上的保存按鈕時,它會自動將表上的現有數據更改為更新的數據,無需重新加載頁面。
這意味着,它需要一個ajax或jquery來更新網頁的各個部分 - 無需重新加載整個頁面,我不知道它是如何工作的。
這是我的表格代碼:
<br>
<button type="button" class="btn btn-info btn-sm pull-right" data-toggle="modal" data-target="#myModal1">Edit</button>
<div class="clear text-primary bold"><i class="fa fa-user text-primary"></i> Basic Information </div>
<br>
<section class="padder-v">
<table class="table table-responsive">
<tbody>
<tr>
<th>
<strong> Employee ID</strong>
</th>
<td>
<p class="text-muted"><?php echo $_SESSION['emp_code']; ?></p>
</td>
<th>
<strong> Birthdate</strong>
</th>
<td>
<p class="text-muted"><?php echo $_SESSION['birthdate']; ?></p>
</td>
</tr>
<tr>
<th>
<strong> Last Name</strong>
</th>
<td>
<p class="text-muted"><?php echo $_SESSION['lname']; ?></p>
</td>
<th>
<strong> Gender</strong>
</th>
<td>
<p class="text-muted"><?php echo $_SESSION['gender']; ?></p>
</td>
</tr>
<tr>
<th>
<strong> First Name</strong>
</th>
<td>
<p class="text-muted"><?php echo $_SESSION['fname']; ?></p>
</td>
<th>
<strong> Marital Status</strong>
</th>
<td>
<p class="text-muted"><?php echo $_SESSION['status']; ?></p>
</td>
</tr>
<tr>
<th>
<strong>Middle Name</strong>
</th>
<td>
<p class="text-muted"><?php echo $_SESSION['mname']; ?></p>
</td>
<th>
<strong> Active</strong>
</th>
<td>
<p class="text-muted"><?php echo $_SESSION['active']; ?></p>
</td>
</tr>
</tbody>
</table>
</section>
php腳本代碼:
<?php
if(isset($_POST['submit'])){
$firstname = $_POST['emp_fname'];
$middlename = $_POST['emp_mname'];
$lastname = $_POST['emp_lname'];
$birthdate = $_POST['emp_bday'];
$gender = $_POST['emp_gender'];
$maritalstatus = $_POST['emp_maritalstatus'];
$query = $mysqli->query("UPDATE tbl_employee SET emp_fname = '$firstname',
emp_mname = '$middlename',
emp_lname = '$lastname',
emp_bday = '$birthdate',
emp_gender = '$gender',
emp_maritalstatus = '$maritalstatus',
WHERE emp_id = '$emp_code'");
if($query){
?>
<div class="alert alert-success alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
<strong>Success!</strong> You Added a New Borrower!
</div>
<?php } else{ ?>
<div class="alert alert-danger alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
<strong>Danger!</strong> Something's wrong with the Process!
</div>
<?php
}
}
?>
模態代碼:
<div class="modal fade" id="myModal1" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"><div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel"> <i class="fa fa-user text-primary"></i> Basic Information</h4>
</div>
<div class="modal-body">
<form method="post" >
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label for="emp_fname">First Name</label>
<input type="text" class="form-control" id="emp_fname" name="emp_fname" value ="<?php echo $_SESSION['fname']; ?>" required>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="emp_bday">Birthdate </label>
<input type="text" class="form-control" id="emp_bday" name="emp_bday" value="<?php echo $_SESSION['birthdate']; ?>">
</div>
</div>
<!--- -->
<div class="col-md-6">
<div class="form-group">
<label for="emp_mname">Middle Name </label>
<input type="text" class="form-control" id="emp_mname" name="emp_mname" value="<?php echo $_SESSION['mname']; ?>">
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="emp_gender">Gender</label><br>
<select class="form-control" name="emp_gender">
<option>Male</option>
<option>Female</option>
</select>
</div>
</div>
<!--- -->
<div class="col-md-6">
<div class="form-group">
<label for="emp_lname">Last Name </label>
<input type="text" class="form-control" id="emp_lname" name="emp_lname" value="<?php echo $_SESSION['lname']; ?>">
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="emp_maritalstatus">Marital Status</label><br>
<select class="form-control" name="emp_maritalstatus">
<option>Single</option>
<option>Married</option>
<option>Divorced</option>
<option>Separated</option>
<option>Widowed</option>
</select>
</div>
</div>
<!--- -->
</div>
<div class="modal-footer">
<button id = "submit" type="submit" class="btn btn-success" name="submit">Save</button>
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
</div>
</form>
</div>
</div>
</div>
從modal發布數據時找不到$emp_code
。 您的查詢將失敗,因為沒有$emp_code
值WHERE emp_id = '$emp_code'
所以你需要添加隱藏的輸入值<?php echo $_SESSION['emp_code']; ?>
<?php echo $_SESSION['emp_code']; ?>
並將其與其他表單值一起發布,因為在PHP文件中。
以模態形式添加
<input type="hidden" class="form-control" id="emp_code" name="emp_code" value ="<?php echo $_SESSION['emp_code']; ?>" >
在PHP文件中添加
$emp_code = $_POST['emp_code'];
UPDATE
另一個問題在你的查詢中,在emp_maritalstatus = '$maritalstatus',
之后還有額外的逗號emp_maritalstatus = '$maritalstatus',
因此更新時會出錯。
我有更新查詢
$query = $mysqli->query("UPDATE tbl_employee SET emp_fname = '$firstname',
emp_mname = '$middlename',
emp_lname = '$lastname',
emp_bday = '$birthdate',
emp_gender = '$gender',
emp_maritalstatus = '$maritalstatus'
WHERE emp_id = '$emp_code'");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.