I have a problem. I have one page that has a slider and a button. On the second page, I would like to read what value the user sent. I tried to do it using cookies and session, but the problem occurs when I want to display the data on the second page. The second page is as if delayed and shows the previous value.
// SOLVED. The answer is in the comment.
Looks like the second page is opening before your ajax call finishes processing. I recommend opening the window to the second page, only after your ajax call returns. Change button code to be:
<button id="button">Open New Tab With SESSION</button>
Change jQuery bits to be:
$(document).ready(function() {
$('#button').click(function() {
$.ajax({
type: 'POST',
url: `${window.location.origin}/wp-admin/admin-ajax.php`,
data: { action: 'set_session_variable', buttonValue: test.value },
success: function(response) {
console.log('SESSION set successfully');
console.log(test.value);
window.open('https://*****.com/second_page/', '_blank');
}
});
});