方法:重定向后获取:没有表单操作的AJAX

时间:2015-12-17 19:37:30

标签: php ajax forms post-redirect-get

我想停止页面刷新的表单重新提交,以便用户配置文件中的表单可以更新一些基本的配置文件信息,并使用ajax请求进行查询。请求工作正常,一切都很好,但如果他们刷新,他们会得到经典的表单重新提交警告,我想在这种情况下避免。我明白这样做你可以使用post-redirect-get,但是我遇到了麻烦,因为我的表单没有“动作”,当点击提交按钮时,它只是重新加载配置文件页面(当前页面),其中包含更新的用户信息DB。

我尝试使用:

if(isset($_POST)) {
   unset($_POST);
   header('Location: profile.php');
}

在当前页面的最顶层之前,但它导致了重定向循环。 在我的情况下有一种简单的方法吗?下面是从ajax请求执行的表单,ajax请求和脚本。

表格:

<form id="info-form" method="post">
            <div class="form-group">
                <label for="location">From:</label>
                <input type="text" class="form-control" name="location" id="fromVal" value="<?php if(isset($location)) echo $location; ?>">
            </div>
            <div class="form-group">
                <label for="gender">Gender:</label>
                <input type="text" class="form-control" name="gender" id="genderVal" value="<?php if(isset($gender)) echo ucwords($gender); ?>">
            </div>
            <div class="form-group">
                <label for="email">Email:</label>
                <input type="text" class="form-control" name="email" id="emailVal" value="<?php if(isset($email)) echo $email; ?>" >
            </div
            <div class="buttonHolder">
                <button id="update" name="submit" type="submit" >Update</button>
                <button type="button" class="action" id="update-cancel" data-dialog-close>Cancel</button>
            </div>  
</form>

AJAX:

$('#update').click(function(){
        var emailVal = $("#emailVal").val();
        var genderVal = $("#genderVal").val();
        var locVal = $("#fromVal").val();
        $.ajax({
            url: 'updateProfile.php',
            type: 'POST', // GET or POST
            data: { email: emailVal, location : locVal, gender : genderVal }, // will be in $_POST on PHP side
            success: function(data) { // data is the response from your php script
                // This function is called if AJAX query was successful
            },
            error: function() {
                // This callback is called if AJAX query has failed
            }
        });
});

updateProfile.php

<?php

require_once 'config.php';

  try {
     $mysqli= new mysqli($host, $username, $password, $dbname); 
     if ($mysqli->connect_error) {
         die('Connect Error (' . $mysqli->connect_errno . ') '
             . $mysqli->connect_error);
      }
  } catch (mysqli_sql_exception $e) { 
      throw $e; 
  }


  $stmt = $mysqli->prepare( "UPDATE users SET email=?,location=?,gender=? WHERE fbuid = ?");
  session_start();
  $stmt->bind_param("sssi", $email, $location, $gender, $_SESSION['fbuid']);

  $email = $_POST['email'];
  $location = $_POST['location'];
  $gender = $_POST['gender'];

  $stmt->execute();
  $stmt->free_result();
  $stmt->close();
  $mysqli->close();

?>

1 个答案:

答案 0 :(得分:0)

页面加载时将始终设置

$_POST(即使它不是POST请求)。

取消设置无法提供帮助,因为当您重定向时,您会加载新页面并重新设置。

Test to see if it is an actual POST request或测试以查看$_POST中是否存在特定的表单数据。

相关问题