【问题标题】:How-To: Post-Redirect-Get: AJAX with no form action操作方法:重定向后获取:没有表单操作的 AJAX
【发布时间】:2015-12-17 19:37:30
【问题描述】:

我想停止在页面刷新时重新提交用户个人资料上的表单,他们可以更新一些基本的个人资料信息,并使用 ajax 请求进行查询。该请求工作正常,一切都很好,但如果他们刷新他们会收到经典的表单重新提交警告,我想在这种情况下避免。我知道要做到这一点,您可以使用 post-redirect-get 但我遇到了麻烦,因为我的表单没有“操作”,当单击提交按钮时,它只需重新加载配置文件页面(当前页面)以及来自更新的用户信息数据库。

我尝试使用:

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();

?>

【问题讨论】:

  • 您实际上并没有在这里使用 Ajax。当点击submit按钮时,JS运行,但表单提交并在发送Ajax请求之前加载了一个新页面。

标签: php ajax forms post-redirect-get


【解决方案1】:

$_POST 将始终在页面加载时设置(即使它不是 POST 请求)。

取消设置不会有帮助,因为当您重定向时,您会加载一个新页面并再次设置它。

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

【讨论】:

  • 谢谢,不知道我怎么没有意识到这一点。我使用了 if(isset($_POST['submit'])) 并且效果很好。我会在 4 分钟内接受答案。
猜你喜欢
  • 1970-01-01
  • 2012-03-11
  • 1970-01-01
  • 2013-07-25
  • 2023-01-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多