-1

In my PHP-Code i have got a variable $password, which i want to edit in a javascript function which is located in a .js File. That means passing $password as a parameter to the function and saving the return value into the variable.

How can I do this?

Kopse
  • 73
  • 1
  • 4

2 Answers2

0

I know you can try to add this to your PHP code:

echo '<script type="text/javascript"> drawChart(); </script>';

and it will access your script, however you should really try to do it with AJAX.

Hope it works for you

0

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'];
}
Gwendal
  • 1,273
  • 7
  • 17