6

Acutally I have a working AJAX Call that get me back after succeed to a static defined Site.

$.ajax({ 
            url: 'myPage.php',
            type: 'get', 
            data: {TestaufstellungID:TestaufstellungID, Datum: Datum}, 
            dataType: 'text',

            success:function(data){ 

                    window.location = "staticPage.php";
                 console.log('SQL Eintrag Erfolgreich Aktuallisiert');
     },
     error: function(jqxhr, status, exception) {
         console.log(exception);


            }
        });

It works fine with window.location but I want a dynamic site, back to this site where the use came from, like in PHP:

header('Location: ' . $_SERVER['HTTP_REFERER']);

Please don't give me answer like history.go(-1); becasue I don't want cached sites. Should be do the same like the PHP code, because in some cases I need the page URL with all string (Get-Method).

My reference Post, to understand the step by step page working order, I want to be back all in all to the first step (page) but this page is not always the same one.

https://stackoverflow.com/qs/56239790

3
  • Why are you using AJAX at all? Commented May 25, 2019 at 12:18
  • Thats just one example Ajax Call. I need calls to the server without reloading. And i edit the post with my reference question to understand the Background.
    – Daniel
    Commented May 25, 2019 at 12:44
  • Are you building a single page application? If yes, you should look into using a client-side router: github.com/krasimir/navigo. If not, why are you loading the previous page's html with an ajax call? Commented Jun 2, 2019 at 12:55

2 Answers 2

5
+50

I'm not certain I understand you completely. However...

$previous = "javascript:history.go(-1)";
if(isset($_SERVER['HTTP_REFERER'])) {
    $previous = $_SERVER['HTTP_REFERER'];
}

For redirecting with PHP

header("Location: $previous");

For testing with a HTML link

<a href="<?= $previous ?>">Back</a>

I hope this is helpful.

3

To Redirect to the Previous worked page.

window.history.back();

or

history.back();

It redirects to the previous page that we used to work. but when you want to redirect to the particular URL you can use that link in the href tag.

click Here for Reference the link tags

Not the answer you're looking for? Browse other questions tagged or ask your own question.