-1

I am developing a web application in php, and I've used javascript many times. Now I need to use a php variable in javascript. For example, a variable generated from a SQL query return, or a session variable, which are needed in the javascript section.

What is the best way to use a php variable in javascript?

Chris
  • 57,622
  • 19
  • 111
  • 137

4 Answers4

7

Maybe you could do something like:

<script>
  var a = <?php echo $a; ?>; //for numbers
</script>

That's one basic way of accomplishing what you want.

EDIT: As JiteshNK also pointed out, you could also do:

<script>
  var a = <?php echo json_encode($a); ?>; //safest solution
</script>
Chris
  • 57,622
  • 19
  • 111
  • 137
  • 1
    Note that the first option only works for numerical values, anything else (including strings) will lead to an error. – jeroen Apr 18 '16 at 07:23
  • 1
    @jeroen, true. You could wrap the php tags in quotes. – Chris Apr 18 '16 at 07:30
  • 2
    Not really, what if the string contains quotes? `json_encode()` is the best / only option. Note that `json_encode()` will put quotes around a string and escape any quotes in it. – jeroen Apr 18 '16 at 07:31
  • 1
    It certainly is the best way, but hey - still an option :) – Chris Apr 18 '16 at 07:33
  • 1
    @jeroen great what you have point here will help beginners. – JiteshNK Apr 18 '16 at 07:36
1

You can do something like this :

If a normal variable:

<script type= "text/javascript">
  var a = <?php echo $var; ?>;
</script>

OR

If you have json :

<script type= "text/javascript">
  var a = <?php echo $json_encode($var); ?>;
</script>
Nehal
  • 1,542
  • 4
  • 17
  • 30
  • If it is a "normal variable", whatever that means, than `var a = ` won't work. The string will either be treated as a variable name or (if it contains, for example, spaces) a syntax error. If you have JSON then encoding it as JSON is pointless. – Quentin Apr 19 '16 at 07:26
0

If its simple string variable then use.

<script>
    var a = "<?php echo $var; ?>";
</script>

If php variable is array then

var resultArray = <?php echo json_encode($varArr); ?> 

// Use resultArray values in javascript
$.each(resultArray, function(index, value) {

}
RJParikh
  • 4,096
  • 1
  • 19
  • 36
  • If it is a simple string variable than `var a = ` won't work. The string will either be treated as a variable name or (if it contains, for example, spaces) a syntax error. – Quentin Apr 19 '16 at 07:26
-1

I don't fully understand what you are trying to do. Basically, I don't thing what you would like to do is possible. Reason: JavaScript is running in the browser. At the time when your JavaScript code is executed, the page has already been loaded. At that time, there is no such thing as PHP available anymore. Your browser only knows about HTML. PHP is running on the server and is used to "construct" or "build" or "compile" your HTML page. What you definitely can do is: You can use PHP to create your JS code dynamically (like you already do that with your HTML file) and there use PHP to populate a JS variable with the contents of a PHP variable.

TomS
  • 467
  • 9
  • 25
  • 1
    What you are saying is correct, but php can output the variable to the script tag when the document is being built, as you put it. Then, js will have that variable when it executes. – Chris Apr 18 '16 at 07:39
  • Well, not quite. it's not the PHP variable. It's a JS variable which might have the same name and has the same value. But the PHP variable does not exist anymore at that time. If JS changes the value of the variable, theres no feedback to any PHP variable, as it does not exist anymore. It's important to understand that in order to understand why you cannot use PHP variables in JS. Btw, if you downvoted my answer, could you please explain, why? – TomS Apr 23 '16 at 12:55
  • There's no interest here to "update" the php variable. All OP wanted was to give the value of a php variable to a js variable - to pass it over, so to speak. No, I didn't downvote btw. – Chris Apr 23 '16 at 13:05
  • well, you may be right. For me it was not very clear (he did not use the word "value" in his question at all). But nevertheless, the question has an accepted answer and your understanding seems to have been correct. That's what counts :-) – TomS Apr 23 '16 at 13:26