Javascript Relocation with Query String or Global

RationalRabbit
RationalRabbit used Ask the Experts™
on
I have a login script, instigated by a button in the header that calls up a form in a modal window which, on submit, processes  using jQuery AJAX, On successful login, the script returns to the original page, where it calls up another modal window with utilities only available to the logged in user. I have tried using a query variable, and I have also tried using a global but, for some reason, both fail.

Code has been working fine to just log the user in and return to the page. The page name is held in a PHP variable:
$CurrentPage = empty($_REQUEST['CurrentPage']) ? $_SERVER['HTTP_REFERER'] : $_REQUEST['CurrentPage'];

Open in new window


In the AJAX result I have tried using the following to attach a query string.
<?PHP
echo('window.location.href = "'.$CurrentPage.'?ET=1";');
or
echo('window.location.search("'.$CurrentPage.'"+"?ET=1");');
echo('window.location.search("'.$CurrentPage.'?ET=1");');
?>

Open in new window

The page needs to reload in order to show the user is logged in in the header.
When I could not get the query to work, (The string does not appear) I tried using a global variable:
<script>
window.ETSHOW = "Active";
<?PHP
echo('window.location.href = "'.$CurrentPage.'");');
?>
</script>

Open in new window


The code that contains the opening of the logged-in user utility menu is contained in the footer of the page, so I did this when attempting to use the query string:
<script>
<?PHP
if(!empty($_REQUEST[ET]))
{
   // Run function to clear the URL, then
   var hUrl    = "etselect.php";
   var hTitle  = "Utilities";
   var w       = 630;
   var h       = 630;
   LVPopup(hUrl,hTitle,w,h); // Opens the model window
}
?>
</script>

Open in new window

Attempting to use a global variable, I have done this:
<script>
if(typeof window.ETSHOW !== 'undefined' && window.ETSHOW != null) 
{
   ETSHOW = null;
   var hUrl    = "etselect.php";
   var hTitle  = "ExpressTRAX";
   var w       = 630;
   var h       = 630;
   LVPopup(hUrl,hTitle,w,h);
}
</script>

Open in new window

I've not used JavaScript globals much, but I do not understand why that is not working, and I am really puzzled that the query variable is not attaching to the URL.

Process Order
(Header and Footer are includes that appear on every page - But not in the modal windows)
  1. User selects the Login button in the header. This opens a login form in a js modal window.
  2. Form submit uses Ajax to authenticate the login.
  3. On success, I need to reload the original page. This changes the Login button to a Logout and should instigate another modal window from js code contained in the Footer. This Modal contains a selection of utilities available to the user.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
You are using AJAX and then you are outputting JavaScript from PHP - that does not make sense.

Why not simply send the page you want to go to back in the AJAX response and in the callback simply do

$.ajax({
...
  dataType: 'JSON',
}).then(resp) {
   window.location.href = resp.currentPage;
});

Open in new window


When you reload the page is re-rendered server side with all the logged on extras.

Your AJAX on login only handles the logon process and the redirect.

Author

Commented:
There's a lot I haven't included here for brevity. The PHP code does some other functions, and should render long before the Ajax is called, so, from my perspective, shouldn't be a problem, but I am moving it out. Unexpected events have pulled me away. Will get back to you as soon as I am able to implement the changes. Obviously, there's something simple I'm completely missing.
Removed the PHP, but that just put me back to square 1, with a better way to get there. It was still not loading the page, and not sending the URL.
Finally, instead of window.location.href, I used window.parent.location.href. (window.parent.location.href = data.CurrentPage+"?ET=1")
That did the trick.

Author

Commented:
Thanks, Julian!
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
Glad you sorted it out.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial