使用php在引导模式对话框中使用编辑表单
所以基本上我有一个员工基本信息表。它已经具有从mysql数据库检索到的值。正如你在桌子上看到的,它在右上角有一个编辑按钮。单击后,将显示模式对话框 我的问题是,一旦用户填写编辑表单上的字段并单击保存按钮,我不知道如何更新基本信息表上的记录 我想做的是,当用户单击模式对话框上的保存按钮时,它会自动将表上的现有数据更改为更新数据,而无需重新加载页面 这意味着,它需要一个ajax或jquery来更新web页面的一部分,而不需要重新加载整个页面,我不知道它是如何工作的 以下是我的表格代码:使用php在引导模式对话框中使用编辑表单,php,ajax,twitter-bootstrap-3,modal-dialog,sql-update,Php,Ajax,Twitter Bootstrap 3,Modal Dialog,Sql Update,所以基本上我有一个员工基本信息表。它已经具有从mysql数据库检索到的值。正如你在桌子上看到的,它在右上角有一个编辑按钮。单击后,将显示模式对话框 我的问题是,一旦用户填写编辑表单上的字段并单击保存按钮,我不知道如何更新基本信息表上的记录 我想做的是,当用户单击模式对话框上的保存按钮时,它会自动将表上的现有数据更改为更新数据,而无需重新加载页面 这意味着,它需要一个ajax或jquery来更新web页面的一部分,而不需要重新加载整个页面,我不知道它是如何工作的 以下是我的表格代码: <br
<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>
编辑
基本信息
员工ID
生日
姓氏
性别
名字
婚姻状况
中间名
活动的
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
}
}
?>
当您从modal发布数据时,找不到$emp\u代码。您的查询将失败,因为没有$emp\u code
值,其中emp\u id='$emp\u code'
所以您需要添加带有值
的隐藏输入,并将其与其他表单值一起发布,因为在PHP文件中
附加模态形式
<input type="hidden" class="form-control" id="emp_code" name="emp_code" value ="<?php echo $_SESSION['emp_code']; ?>" >
更新
另一个问题是在您的查询中,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'");
开始时,您需要添加隐藏的输入值
,并将其与其他表单值一起发布,因为在PHP文件中,更新查询将失败,因为没有$emp\u code
值,其中emp\u id='$emp\u code'
请检查我的答案,它可能对您有效。我没有把它包括在问题中
$emp_code = $_POST['emp_code'];
$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'");