0
index.js // linked to all php files.
var pass;
console.log(pass); // undefined

$("#btn").click(function() {
    pass = "323";
    location.href = "test.php";
});

going to test.php console.log shows again undefined.
I need to keep pass value as 323 accross all pages, once it is estblished.

1
  • use cookies or localstorage or even sessions Commented Feb 3, 2016 at 5:12

2 Answers 2

1

Simplest method: Use localStorage.

If you only need to store strings:

var pass = localStorage.pass;

$("#btn").click(function() {
    pass = "323";
    localStorage.pass = pass;
    location.href = "test.php";
});

Or, notice that localStorage will change all things you store in into string. So if you wanted to store object, numbers, etc, you need JSON.stringify.

// index.js
if (localStorage.pass === undefined)
    localStorage.pass = "null";
var pass = JSON.parse(localStorage.pass);

$("#btn").click(function() {
    pass = "323";
    localStorage.pass = JSON.stringify(pass);
    location.href = "test.php";
});
Sign up to request clarification or add additional context in comments.

Comments

1

You can use the session to get your things work smoothly on all pages.

var pass;
console.log(pass); // undefined

$("#btn").click(function() {
   pass = "323";
   $.post('savesession.php','pass='+pass,function(){
       console.log("session saved");
       location.href = "test.php";
   });
});

savesession.php

<?php
session_start();
$pass = $_POST['pass'];
$_SESSION['pass'] = $pass;

anypage.php

<?php
session_start();
$pass = $_SESSION['pass'];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.