简体   繁体   English

php标头不适用于ajax。 用JS重新加载的问题

[英]php header does not work with ajax. Issue with reloading with js

So I am using an ajax request to post data into a php script. 所以我正在使用ajax请求将数据post到php脚本中。 At some point, the php script redirects. 在某些时候,PHP脚本会重定向。

Well at least it should be. 至少应该如此。 But it does all kind of weird things 但这确实很奇怪

header('Location: index.php');

It get's all the content in index.php and puts it in the callback area. 它获取index.php中的所有内容,并将其放在回调区域中。

So I decided to reload the page using javascript. 因此,我决定使用javascript重新加载页面。 And this is what I got 这就是我得到的

$.post('booking.php',
{
    firstname : $('#firstname').val().trim(),
    lastname : $('#lastname').val().trim()
},function(data){   
        window.location = 'index.php';
        $('.notification-booking').html(data);
});

Problem is. 问题是。 There is some feedback being given. 有一些反馈。 What I have up there displays the feedback for a moment then reloads the page. 我在那里的内容会显示反馈一会儿,然后重新加载页面。 Which woukd be cumbersome if the user hasn't finished reading the text. 如果用户还没有阅读完文本,那会很麻烦。 So I would like to do the following. 因此,我想做以下事情。

  1. reload the page, 重新加载页面,
  2. display the feedback 显示反馈
  3. Eat a donut and be happy. 吃一个甜甜圈,并开心。

In exactly that order I have no. 我完全没有那个顺序。 3 under control. 3在控制之下。 Help out on number 1 帮助第一

You could pass variables to the second page via JavaScript through the URL, then retrieve them via PHP. 您可以通过URL通过JavaScript将变量传递到第二页,然后通过PHP检索它们。 That way you can display information for as long as the user needs to read it. 这样,您可以在用户需要阅读的时间内显示信息。

Javascript on first page 第一页的Javascript

window.location = 'second.php?firstVar=' + firstname + '&secondVar=' + lastname + ';

Then inside second.php, you can retrieve the value of those variables, and display them as you please. 然后在second.php中,您可以检索这些变量的值,并根据需要显示它们。

<?php
$first = $_GET['firstVar'];
$last  = $_GET['secondVar'];
?>

I recommend you redirect the user via Javascript instead of PHP headers because according to this thread it's really easy to screw up PHP headers. 我建议您通过Javascript而不是PHP标头重定向用户,因为根据此线程 ,很容易搞砸PHP标头。 I'm assuming that's why you're getting weird behavior. 我假设这就是为什么您会得到奇怪的行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM