更新数据后重定向

时间:2017-05-03 16:44:00

标签: php html mysql

我正在我的网站上创建一个管理区域,用户可以在我的数据库中的表格中编辑详细信息。

我使用bootstrap模式来存放我的更新表单,并使用ajax和php来动态加载数据。

点击更新后,我需要帮助重定向回管理页面。

当我点击更新时,我得到的是insert.php中的echo消息,我想重定向回suadminUsers.php。

suadminUsers.php:

<div id="edit_data_Modal" class="modal fade">  
  <div class="modal-dialog">  
       <div class="modal-content">  
            <div class="modal-header">  
                 <button type="button" class="close" data-dismiss="modal">&times;</button>  
                 <h4 class="modal-title">PHP Ajax Update MySQL Data Through Bootstrap Modal</h4>  
            </div>  
            <div class="modal-body">  
                 <form method="post" id="insert_form" action="insert.php">  
                      <label>Enter Username</label>  
                      <input type="text" name="username1" id="username1" class="form-control" />  
                      <br />  
                      <label>Enter Password</label>  
                      <input type="text" name="password1" id="password1" class="form-control" />  
                      <br />  
                      <label>Enter Role</label>  
                      <input type="text" name="role1" id="role1" class="form-control" />  
                      <br />  
                      <input type="hidden" name="user_id" id="user_id" />  
                      <input type="submit" name="insert" id="insert" value="Insert" class="btn btn-success" />  
                 </form>  
            </div>  
            <div class="modal-footer">  
                 <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>  
            </div>  
       </div>  
  </div>  
</div>

<script>  
 $(document).ready(function(){  
  $('#edit').click(function(){  
       $('#insert').val("Insert");  
       $('#insert_form')[0].reset();  
  });  
  $(document).on('click', '.edit_data', function(){  
       var user_id = $(this).attr("id");  
       $.ajax({  
            url:"fetch.php",  
            method:"POST",  
            data:{user_id:user_id},  
            dataType:"json",  
            success:function(data){  
                 $('#username1').val(data.username);  
                 $('#password1').val(data.password);  
                 $('#role1').val(data.role);  
                 $('#user_id').val(data.id);  
                 $('#insert').val("Update");  
                 $('#edit_data_Modal').modal('show');  
            }  
       });  
  });  
  $('#insert_form').on("submit", function(event){  
       event.preventDefault();  
       if($('#username1').val() == "")  
       {  
            alert("Username is required");  
       }  
       else if($('#password1').val() == '')  
       {  
            alert("Password is required");  
       }  
       else if($('#role1').val() == '')  
       {  
            alert("Role is required");  
       }  
       else  
       {  
            $.ajax({  
                 url:"insert.php",  
                 method:"POST",  
                 data:$('#insert_form').serialize(),  
                 beforeSend:function(){  
                      $('#insert').val("Inserting");  
                 },  
                 success:function(data){  
                      $('#insert_form')[0].reset();  
                      $('#edit_data_Modal').modal('hide');
                      $('#user_table').html(data);
                 }  
            });  
       }  
  });   
});  
</script>

insert.php:

<?php  
 $connect = mysqli_connect("localhost", "root", "", "stone_store");  

 if($_POST["user_id"] != '')  
  {  
   $message = 'Data Updated';  
   $username1 = mysqli_real_escape_string($connect, $_POST["username1"]);  
   $password1 = mysqli_real_escape_string($connect, $_POST["password1"]);  
   $role1 = mysqli_real_escape_string($connect, $_POST["role1"]); 
   $query = "  
       UPDATE users   
       SET username='$username1',   
       password='$password1',   
       role='$role1'
       WHERE id='".$_POST["user_id"]."'";   

   if(mysqli_query($connect, $query))
    {  
        echo $message;
    }  

    else  
     {  
         echo "ERROR: Could not able to execute $query. " . 
         mysqli_error($connect);
     }

  }  
?>

fetch.php:

  <?php  
   //fetch.php  
   $connect = mysqli_connect("localhost", "root", "", "stone_store");  
   if(isset($_POST["user_id"]))  
    {  
     $query = "SELECT * FROM users WHERE id = '".$_POST["user_id"]."'";  
     $result = mysqli_query($connect, $query);  
     $row = mysqli_fetch_array($result);  
     echo json_encode($row);  
    }  
  ?>

1 个答案:

答案 0 :(得分:2)

您可以使用:

window.location.href="Url you want to redirect" ;

window.open("url","_self");

来自JS的

,您可以在ajax中捕获成功响应