user2429302
user2429302

Reputation: 89

How to pass global variable in javascript function

Let say I have a $_SESSION['password'] and a javascript function.

Here's the javascript function:

<script>  
    function check() {
        var password = '<?php echo $_SESSION; ?>';
        alert(password);
        if (document.FormName.password.value != password)
            alert("password does not match");
    } 
</script>

<html>
//form here
</html>

How come when the alert pop-ups, it will only display nothing? what happened? Is my passing of variable wrong at all?

Upvotes: 0

Views: 182

Answers (2)

Bryan
Bryan

Reputation: 3494

<script>
    function check() {    
        var password = "<?=$_SESSION['password']?>";//you forgot ['password'] here       
        alert(password);
        if(document.FormName.password.value != password)
            alert("password does not match");
    } 
</script>

<html>
//form here
</html>

Upvotes: 0

Code Lღver
Code Lღver

Reputation: 15603

$_SESSION is an array and this will return blank if you echo it.

You should use the

<?php echo $_SESSION['password']; ?>

It will echo the password that store in Session.

Upvotes: 1

Related Questions