php - 使用 php 在引导模式对话框中处理编辑表单

标签 php ajax twitter-bootstrap-3 modal-dialog sql-update

所以基本上我有一张员工基本信息表。它已经具有从 mysql 数据库中检索到的值。 employee basic information table .正如您在表格中看到的那样,它的右上角有一个编辑按钮。单击后,将显示模态对话框。 edit basic information modal .

我的问题是,一旦用户填写了编辑表单上的字段并单击保存按钮,我不知道如何更新基本信息表上的记录。

我想要发生的是当用户点击模态对话框上的保存按钮时,它会自动将表格中的现有数据更改为更新后的数据,无需重新加载页面。

也就是说,它需要一个 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">&times;</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">&times;</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">&times;</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>

最佳答案

$emp_code从模态发布数据时未找到。您的查询将失败,因为没有 $emp_codeWHERE emp_id = '$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'];

更新

另一个问题在您的查询中, 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 - 使用 php 在引导模式对话框中处理编辑表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34566351/

相关文章:

javascript - IE8正在将表单提交到当前页面

javascript - 在流程图中传递ajax参数

php - 通过composer命令在Laravel安装程序中出错

php - 输出jquery UI slider 到mysql无法获取PHP文件来获取POST数据

php - 如何将数组的每两个连续值转换为键/值对?

jquery - 引导日期选择器突出显示两个日期

javascript - 使用 jquery 更新多个 anchor aysc 时遇到问题

javascript - 显示多个文件上传的进度 Jquery/Ajax

html - Bootstrap 表格必填字段

css - Twitter Bootstrap 3 - 在词缀滚动上丢失容器全宽