-3

When using an XMLHttpRequest() I'm unable to get the header() function to work as a 302 redirect, why is this?

header('location: example/example.php', true, 302)

  • this is **by no means** a "minimal" example. Asking someone to set up such a working environment to answer your question is beyond reason – Your Common Sense Aug 16 '23 at 19:48
  • @YourCommonSense Minimal doesn't mean less than X amount of lines, it means 'what is relevant to the problem and the rest deleted' everything shown is relevant to the problem. This is only a couple hundred lines of code I've seen a whole lot more. – Azur_Was_here Aug 16 '23 at 19:52
  • YES, it means "what is relevant". and 99% of this code is **not**. You are asking about header function, so it must be a **single header function** in your reproducible example. – Your Common Sense Aug 16 '23 at 19:55
  • @YourCommonSense I'm asking what in this chunk of code is causing a 302 temporary redirect header to spit out page content in the AJAX response text rather than redirecting like it should. Not what a header function does. – Azur_Was_here Aug 16 '23 at 20:02
  • Hols on, not that fast. Your question title explicitly says, "Why Is The PHP Header Function Echoing Page Content". So you must provide that PHP Header Function that's echoing your HTML. – Your Common Sense Aug 16 '23 at 20:07
  • 1
    by the way, while we're at it. This entire database class is **worse** than vanilla PDO. Compare that code of yours with [only three lines you need with PDO](https://phpize.online/sql/mysql57/undefined/php/php81/a9eb4cf7ebcbb893cd0cca112c6645c0/). – Your Common Sense Aug 16 '23 at 20:10
  • @YourCommonSense did you even read the code? I literally gave the header code that is doing that. – Azur_Was_here Aug 16 '23 at 20:12
  • 1
    Nope I did not. I have explained already that the code is WAY too big. You need to provide a reliable minimal reproducible example if you want people to read your code. – Your Common Sense Aug 16 '23 at 20:13
  • @YourCommonSense So let me get this straight, you did not A) Read the code, B) look for highlighted things that are marked as important C) Read The Beginning Of The Text. You just simply scrolled through and said 'Ah! Too Big' – Azur_Was_here Aug 16 '23 at 20:16
  • It doesn't matter actually, whether I read or not. "V" in MCVE is not about reading. It's about running the code and confirm the results. Your code is not runnable. – Your Common Sense Aug 16 '23 at 20:21
  • @YourCommonSense see new example. – Azur_Was_here Aug 16 '23 at 20:23
  • Well, now it's much batter! I can reproduce this issue and tell you how to fix it right away! In order to execute the PHP code, you must add a PHP tag to it. So simply add ` – Your Common Sense Aug 16 '23 at 20:30
  • @YourCommonSense apologies see new example. – Azur_Was_here Aug 16 '23 at 20:33
  • This new example is not reproducible. I tried it, and got 404 error. By the way, what particular content you are talking about? – Your Common Sense Aug 16 '23 at 20:37
  • by the way why you're using header() with XMLHttpRequest()? What outcome you expect? – Your Common Sense Aug 16 '23 at 20:43
  • If nothing else, you have to `exit` (or otherwise stop the rest of your code from executing) after a `header('Location')` call. AJAX calls won't follow that header at all, though, not without special handling in your JS code. – ceejayoz Aug 16 '23 at 20:43
  • @YourCommonSense Problem solved, AJAX calls won't follow the header. HTML form submission will though ceejay actually was helpful rather than condescending. – Azur_Was_here Aug 16 '23 at 21:19
  • Why has the question been nominated for reopening when the last comment from the OP says that the problem was solved followed by a summary of the answers to the duplicate question? – Quentin Aug 25 '23 at 21:26
  • @Quentin probably because stack overflow is asking me to rewrite my questions and I am being FORCED to do this or I cant interact with content. Also -1 me big sad this is literally why I don't rewrite questions – Azur_Was_here Aug 25 '23 at 23:25

0 Answers0