从模态表单中检索值并更新数据库php

时间:2017-03-05 17:38:38

标签: javascript php jquery mysql twitter-bootstrap

不确定我是否正确地解决了这个问题,但基本上我尝试使用模态编辑表格中的行。到目前为止,我有3个类是createUser.php,它有表并处理检索行的请求并将数据传递给getuser.php。这将显示模态,现在我尝试从模态表单中检索值并相应地更新数据库。这就是getuser.php看起来像atm:

    <script type="text/javascript">
   $(document).ready(function(){

$("#edit-user").submit(function(e){

 e.preventDefault();

var uid = $('input[name="userid"]').val();
var username = $('input[name="username-update"]').val();
var password = $('input[name="password-update"]').val();
var role = $('input[name="role-update"]').val();

$.ajax({
  url: 'updateUser.php',
  type: 'POST',
  data: {
  id : uid, 
  username: username,
  password: password, 
  role: role
  },
  dataType: 'html'
 })
 .done(function(data){
  console.log(data); 
  $('#update-username').html(data.username);
  $('#update-password').html(data.password);
  $('#update-role').html(data.role);
})

}); 

</script>   
 <?php

  require_once 'database.php';


if (isset($_REQUEST['id'])) {

 $id = intval($_REQUEST['id']);
 $query = "SELECT * FROM users WHERE userid=:id";
 $stmt = $db->prepare($query);
 $stmt->execute(array(':id'=>$id));
 $row=$stmt->fetch(PDO::FETCH_ASSOC);
 extract($row);
  ?>

    <div class="modal-dialog">
        <!-- Modal content-->
        <div class="modal-content row">
            <div class="modal-header custom-modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                <h4 class="modal-title">Edit Staff User Details</h4>
            </div>

            <div class="modal-body">


                                <form name="info_form" class="form-inline" action="createUser.php" method="post">
                                    <input type="hidden" name="userid" id="update-id" value="<?php echo $_REQUEST['id'] ?>">
                    <div class="form-group col-sm-12">
                                                <label>Username</label>
                                                    <input type="text" class="form-control" name="username-update" id="update-username" value="<?php echo $username ?>">
                    </div>
                    <div class="form-group col-sm-12">
                                                <label>Password</label>
                        <input type="text" class="form-control" name="password-update"  id="update-password" value="<?php echo $password ?>">
                    </div>
                    <div class="form-group col-sm-12">
                                                <label>Confirm Password</label>
                        <input type="text" class="form-control" name="confirmpass" id="update-confirmpassword" placeholder="Confirm Password">
                    </div>
                                            <div class="form-group col-sm-12">
                                                    <label>Role</label>
                                                    <select class="form-control" name="role-update" id="update-role" class="field-select">
                                                    <?php if($role == "staff"){ ?>
                                                    <option value="staff">Staff</option>
                                                    <option value="admin">Admin</option>
                                                    <option value="customer">Customer</option>
                                                    <?php }
                                                    else if($role == "admin") { ?>

                                                    <option value="admin">Admin</option>
                                                    <option value="staff">Staff</option>
                                                    <option value="customer">Customer</option>
                                                    <?php } else{ ?>
                                                    <option value="customer">Customer</option>
                                                    <option value="staff">Staff</option>
                                                    <option value="admin">Admin</option>
                                                    <?php } ?>

                                                    </select>
                                            </div>

                    <div class="form-group col-sm-12">
                        <button type="submit" class="btn btn-default pull-right" id="edit-user">Update</button>
                                                    <input type="hidden"    name="submit" value="TRUE" />
                    </div>

                </form>


                            </div>

        </div>

    </div>


<?php
 }
  ?>

另外我应该提一下,由于一些奇怪的原因,我必须两次编写该模态代码(一次在createUser.php然后再在getuser.php),否则模态不会弹出。这是我在updateUser.php中的SQL代码,我希望将数据发送到该代码。

<?php

require_once 'database.php';

  if(isset($_REQUEST['id'])){
     $row = intval($_REQUEST['id']);
     $updateUsername = $_REQUEST['username'];
     $updatePassword = $_REQUEST['password'];
   //$confirmpass = $_REQUEST['confirmpassword'];
     $updateRole = $_REQUEST['role'];

     $query = "UPDATE users SET username=$updateUsername,   password=$updatePassword, role=$updateRole WHERE userid=$row ";
    $stmt = $db->prepare($query);
    $stmt->execute();


}


 ?>

任何人都可以看到我出错的地方吗?还应该提到我在这里学习新东西。此外,当我检查检查元素时,表单数据实际上是在提交时发送的。任何帮助深表感谢! :d

1 个答案:

答案 0 :(得分:0)

我认为你需要像这样的对象发送数据:

data: {
      id : uid, 
      username: username,
      password: password, 
      role: role
  }

我认为您的选择器也是错误的,试试这个:

 var uid = $('input[name="userid"]').val();
 var username = $('input[name="username-update"]').val();
 var password = $('input[name="password-update"]').val();
 var role = $('input[name="role-update"]').val();