0

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?

5
  • 5
    Seriously? you alert the password back? Commented Jun 11, 2013 at 5:33
  • $_SESSION is an array. Commented Jun 11, 2013 at 5:33
  • Check the resultant html in browser. May be password is not set in SESSION. And you did not echo $_SESSION['password'] like you say in question. Commented Jun 11, 2013 at 5:34
  • see how to show code in question Commented Jun 11, 2013 at 5:35
  • This is very wrong. Please don't ever write out the password along with the HTML sent to the client. Commented Jun 11, 2013 at 5:37

2 Answers 2

1

$_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.

Sign up to request clarification or add additional context in comments.

1 Comment

Concise and explains the problem. +1
0
<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>

1 Comment

how come I still come up with an error after doing just this var password = '<?php echo $_SESSION['password'] ?>'; alert(password); alerts nothing

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.