0

I need to redirect to another page in my PHP code on an AJAX call. The only thing I can seem to find online about this is to do it through JavaScript using window.location.href = 'url'; but it does not work.

If I try to redirect using the window.location.href to profile.php?user=person then it has the url localhost/profile.php?user=person but if I type in the full URL for the redirect like project/login/profile.php?user=person then it stays on the same page. The profile.php page only echo's what who the user in the url is.

Any ideas?

Here is my code:

$.ajax({
        url: 'processes/login.inc.php',
        type: 'post',
        data: data,
        success: function(res) {
            if(res != '4502') {
                var s = 'profile.php?user='+res;
                window.location.href = s;
            }

        }
    });

and the PHP:

<?php
require_once('../core/init.php');
$username = escape($_POST['ul']);
$password = escape($_POST['pl']);

$user = new User();

$login = $user->login($username, $password);

if($login) {
    // Redirect::to('../profile.php?user='.$username);
    echo $username;
} else {
    echo '4502';
}
user3241507
  • 353
  • 6
  • 18
  • Why don't you actually show the code you tried? – epascarello Apr 07 '14 at 17:26
  • You can redirect to another page using php `header('Location: http://www.example.com/');` http://www.php.net/manual/en/function.header.php – Scott Apr 07 '14 at 17:27
  • @Scott I tried that, it won't redirect in the PHP while I am calling the PHP file via AJAX – user3241507 Apr 07 '14 at 17:31
  • ok so your trying to redirect the page in the middle of an AJAX request? Try this http://stackoverflow.com/questions/6910291/change-page-in-the-middle-of-ajax-request – Scott Apr 07 '14 at 17:39

1 Answers1

0

You have to assign the full path, not only the php file and parameters, for example:

Change:

location.href = 'profile.php?user=person';

To:

location.href = '/login/profile.php?user=person';
Markus Kottländer
  • 8,228
  • 4
  • 37
  • 61
Gilberto Avalos
  • 502
  • 3
  • 7