0

I don't succeed to change the location (URL) with the "header('Location: URL');"

I've a form in a modal, I submit with Jquery $.post method for create a new user, in the controller before creating the User from POST data, I check if the mail or the name is free. So I've made a simple if-else control with some location for showing an error from the URL GET. On the paper, it was simple and even if not optimized, must working fine, no need more work for this project.

expected:

If user exist:
   reload with user error in the URL;
elseif mail exist:
   reload with mail error in the URL;
else:
   createNewUser(data);

Real code:

if(User::userExist($userName)){
   header('Location: index.php?action=users&error=userAlreadyExist');
   die();
}

I want to reload page/url with new data error for display it to the user.

Atm the post is okay(data received, the ifelse control is okay), but the code dont relocate with the header.

I've tried without Jquery and pass it with a normal post/submit, but it's the same problem.

In these 2 cases, I've a "magic" behavior. When I change the location, with for example: header('Location: index.php?action=loginFail');

The page doesn't relocate, but if I refresh (F5) the page, she going on this location. It's not the case with wanted URL, so just the question, why?

I can modify the global behavior for making it working. But take times for no real gain on this project (It's a basic student project with just php requirement, that part is already more than what is expected)

But I really want to understand why the header dont work here?

Not sure if all is clear... I'll try to explain at my best but I'm a little confused.

  • A `Location` header in response to an AJAX request won't do anything. It can redirect the AJAX request, but since that's happening in the background using Javascript, it's not going to change the page the user is on! If it doesn't work when plainly accessing the URL directly in the browser either, check your error logs and read https://stackoverflow.com/a/8028987/476. – deceze Jun 19 '19 at 06:24
  • I'll go to read your suggestion It's what I first thinked (Dont work with AJAX) that's why I've change with a normal form/post, but that doesn't change anything where in all others page that work fine. – Toquey SiGauses Jun 19 '19 at 06:30
  • You need to think about it in terms of a RESTful API server-side and a client browser-side. The API must have certain response codes which indicate certain outcomes; perhaps you'll actually want to use HTTP status codes, or more informal JSON responses like `{"status": "ok"}`. When the client receives such a response, it decides what to do; redirecting to a different site may be one of those things it does. Whether you hardcode the URL it'll redirect to in the client, or send that URL from the server to have a bit more flexibility is somewhat up to you. – deceze Jun 19 '19 at 06:46
  • I'm still confused about why one of these URL work and the other not. In all case, I think it was clearly not fine to work like that. I change the process, better to work with a fully AJAX component than mix and do some garbage... – Toquey SiGauses Jun 19 '19 at 06:52

0 Answers0