1

Here is my code that redirects user to another page

$.ajax({
    type: "POST",
    url: "ajax/ajax-send-otp.php",
    data: {
        mynumber: mynumber,
        userCountry: userCountry
    },
    success: function(data) {
        if (data == 2) {
            $("#otpVerifyDiv").slideDown(500);
            window.location = 'free-listing-new.php';
        } else {
            alert(data);
        }
    }
});

Now On Another Page There is A Div-Id With #otpVerifyDiv which is hidden. so i wants to show that div. so is it possible to show div after window location. ?

I don't need any parameters to be send with url for some reasons.and also do not need ay other js like jquery-cookies.js i tried

success: function(data) {
    if (data == 2) {
        //SHOW DIV 
        $("#otpVerifyDiv").slideDown(500);
        //THAN REDIRECT
        window.location = 'free-listing-new.php';
    } else {
        alert(data);
    }
}

My HTML

<div class="form-group" id="otpVerifyDiv" align="center" style="display: none;">
 //Some Contetnt Here
</div>

So is it possible ?

My Plan

is jquery provide any temporary storage or else so where i store data and send data from one page to another like cookies session.

  • you would have slide it down in the document ready of the page you are redirecting to – Pete Feb 13 '18 at 11:41
  • where is the code of the other page? the redirected one – zb22 Feb 13 '18 at 11:43
  • @Webster TP : "free-listing-new.php" in this file please add this code $( document ).ready(function() { $("#otpVerifyDiv").slideDown(500); }); – Abhijit Feb 13 '18 at 11:46
  • So what if request isnt send. still it will be shown. and i dont want that. i just want whenever request would be submitted –  Feb 13 '18 at 11:47

5 Answers5

1

First Approach:

Setting a localstorage variable on success:

if (data == 2) {
    localStorage.setItem("otp_verified", 1);
    window.location = 'free-listing-new.php';
}

Then on free-listing-new.php check:

if ( localStorage.getItem("otp_verified") == 1 ) {
    $("#otpVerifyDiv").slideDown(500);
    localStorage.removeItem("otp_verified");
}

Second Approach:

On success, post a variable to free-listing-new.php by creating a hidden form with JS:

if (data == 2) {
    $('body').append($('<form/>', {
        id: 'hiddenForm',
        method: 'POST',
        action: 'free-listing-new.php',
        style: "display:none;"
    }));

    $('#hiddenForm').append($('<input/>', {
        type: 'hidden',
        name: 'otp_verified',
        value: 1
    }));        

    $('#hiddenForm').submit();                
}

Then on free-listing-new.php check posted variable with PHP's super global $_POST['otp_verified']

Sunny Soni
  • 182
  • 2
  • 11
  • Its Really Great Answer. is this supports in all browser –  Feb 15 '18 at 06:02
  • Ensure your browser support localstorage, in fact all of latest browsers do. Second solution will definitely work. – Sunny Soni Feb 15 '18 at 11:31
  • check [localStorage Compatibility](https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage#AutoCompatibilityTable) – Sunny Soni Feb 20 '18 at 10:16
0

Since you using php, you can use condition on the header of the page to prevent people from visiting the page unless they are redirected from a specific page. on the page they are redirecting to, then you have to use the code as stated by Abhijit

0

You may do this by checking that user came from which url by $_SERVER['HTTP_REFERER']. if you get the free-listing-new.php in HTTP_REFERER you can show the your div which you want.

prakash tank
  • 1,269
  • 1
  • 9
  • 15
0

I think you can set local/session storage on succes if the redirect page is in same domain. On coming to the redirected page check that storage data and display the div.

user1162084
  • 462
  • 1
  • 6
  • 18
0

On the success response, you should use localStorage.setItem("key", "value"); instead of cookie/session. This function will store the value temporarily in your browser storage.

Usage : store value of otp_verified on success side and redirect to free-listing-new.php page and check for value using localStorage.getItem("key"); and then remove the item from local storage using localStorage.removeItem("key");.

Set item in local storage.

localStorage.setItem("key", "value");

Get item from local storage

localStorage.getItem("key");

Remove Item from local storage

localStorage.removeItem("key");
Pankaj Makwana
  • 3,030
  • 6
  • 31
  • 47
  • Thankyou Your answer would be useful.Where the value stored if i use localstoreage. is it similler to cokkies ? –  Feb 15 '18 at 06:03
  • Check this link https://stackoverflow.com/questions/8634058/where-the-sessionstorage-and-localstorage-stored – Pankaj Makwana Feb 15 '18 at 06:11