2

Hello Im trying to pass value stored in php to javascript code.

I try to pass the value in $_SESSION[user]

If I have this script in my header:

<script>

var user = ? //How to pass the value?

</script>

In my buttons I do something like this:

onclick="foo('<?php session_start(); echo $_SESSION[user];?>')

But how do I pass it without the user click?

Thanks for helping

EDIT

My JS function located in other file and I reference them by this code in my header:

<script src="class_functions"></script>

How do I pass the same parameter to the other file?

1

5 Answers 5

5

Just echo the PHP value out like you would any content:

<script>
    var user = '<?php echo $_SESSION[user]; ?>';
</script>

JavaScript is just client-side code like HTML is.

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

6 Comments

Or the even shorter ` var user = '<?= $_SESSION[user]; ?>';`
Yep. Be careful though. Newbies will tell you to never use short tags!
Its working thanks. But now i have another problem please see my edit, thanks.
Would be a good idea to check for the session index to be set though.
@geekynotsilly That would only be necessary if this was not behind some sort of login. If you've authenticated the user then you can be sure that information exists.
|
4

If you're PHP script is rendering the page, I would go with the script tag approach.

<head>
   <script type="text/javascript">
       var user = '<?php echo $_SESSION["user"]; ?>';
   </script>
</head>
<body>
</body>

Comments

2

Now that you have added an additional factor to your question, it is a little bit more interesting. If you need to set a variable when the functions that use this variable are located in a different file, then you need to decide the scope of the variable you are passing. There are several options; I will just list two:

  1. Global scope. That is, the variable will be "known" to the other functions because of where it it located. You want to be careful of this, but if that's your approach you can use any of the answers given, e.g.

    <script>
    var x = '<?= $_SESSION["user"];>';
    </script>
    
  2. Local scope. Now you will need to include a function in class_functions that sets the variable. You might end up with something like

    <script>
      set_user('<?= $_SESSION["user"];>');
    </script>
    

Where the set_user() function is defined in your other file, and ensures that the variable is available to the other functions with the correct scope.

I would prefer using method 2 - it is much cleaner.

1 Comment

Thanks. I used the set_user :)
1

I think this is better:

<script>
    var user = '<?php echo htmlentities($_SESSION["user"]); ?>';
</script>

Because if there are quotes $_SESSION['user'], you will get an error. For example if you have:

$_SESSION['user'] = "something with 'quotes'";

the js would like so:

var user = 'something with 'quotes'';

Which is incorrect.

2 Comments

You shouldn't use addslashes(). You should use htmlentities().
You should avoid short_open tags for your example because it is configuration depended if it works or not. And I agree use htmlenitites()
0

You Can use Jquery ,if you want it to happen automatically.

just Use the following code inside your tags

$(document).ready(function()
{
    var user= <? echo $_SESSION['user']?>;
    //Code Goes here......

});

Hope that solves Your Problem

Note:This can be one of the many possible solutions

6 Comments

jQuery is JavaScript. If one include jQuery only to add an onload event, for which one do not need jQuery, one have to seriously rethink the approach.
I know jquery is JAVASCRIPT, the question was about to pass PHP value to JavaScript without buttons.......and this can be one of the ways to pass the data
You do not need to wait for DOM to set a JavaScript variable.
Unless user is a number or some literal like false or null etc., it will also break the script.
You can check for null or other conditions using if statement
|

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.