Here is how to do it with jQuery and Ajax :
getPassword.php
<?php
session_start();
$_SESSION['password'] = "whatever";
header("Content-Type: application/json");
echo json_encode(array("password" => $_SESSION['password']));
?>
Javascript part, don't forget to include jQuery
// Let's get the password !
$.ajax({url: "http://yoursite/getPassword.php", dataType: "JSON"}).done(function(response) {
// Stocking the password here
var password = response.password;
// Do whatever you wan't with the password
password = processPassword(password);
// Post the password to php
$.post("http://yoursite/setPassword.php", {password: password});
});
And finally, setPassword.php
<?php
session_start();
if (!empty($_POST['password'])) {
// Retrieving the password sent by ajax in session
$_SESSION['password'] = $_POST['password'];
}