Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

This question already has an answer here:

I want to pass "$var_external" to the Javascript "SHOW".

HERE I call the function:

<?php
include("test23.php");

$call_function = test23(); 
?>

THIS is the function test23.php:

<?php

function test23() {

$var_external = 9999;       // This doesn't work

print("<a href=\"dummy\" id=\"SHOW\">Activate script</a>");

}
?>    

<script type="text/javascript">

$( function () {
  $("a#SHOW").toggle( function() {  

    var var_external =  <?php echo $var_external; ?>;

    alert(var_external);

  }, 
  function () {
    return false;
  });
});

If you click on "Activate script" the $var_external should be passed to Javascript.

When $var_external is set inside the PHP-function it doesn't work.

When I put $var_external outside the PHP-function it works.

Why doen't it work inside the PHP-function?

share|improve this question

marked as duplicate by Marcel Korpel, Mike W, hexacyanide, rink.attendant.6, Josh Crozier Nov 9 '13 at 21:34

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
When echo'ing variables to JavaScript in PHP, always use json_encode to properly encode the variable, otherwise you're not safe. –  Marcel Korpel Nov 9 '13 at 19:54
    
I don't need a global variable. That was just a try to get it work, but it didn't. I skipped the global statement now, because that was not the point. –  Eduard Nov 9 '13 at 20:03

2 Answers 2

up vote 0 down vote accepted

Don't use global variables, it's a poor practice to get in the habit of.

You should pass the value into the function as an argument:

function test23( $var_external ){
  // do stuff to modify it
  return $var_external;
}

Then print the result:

<?php print test23($var_external); ?>

It's hard to give better advice because I don't know what the values are really for, and why they're being modified.

A few other tips:

  • The variable has to created outside the function before global would work.
  • Be sure a value is always printed, if it prints an empty/null val, the javascript will break. Or, use quotes and then convert to a number or whatever you need
share|improve this answer
    
I don't need to global $var_external. That was just a guess to get it work, but it didn't –  Eduard Nov 9 '13 at 19:52
    
It doesn't matter if you need it global or not. The PHP echo statement you use, assumed it's outside a function. Either ditch the function in the script, or change the echo statement to call the function instead. My example shows that recommendation –  helion3 Nov 9 '13 at 19:55
    
I removed the Java Script code from the PHP-function and put it in the call to the PHP-function. Now it's clearer that the variable must be returned from the PHP-function. –  Eduard Nov 10 '13 at 13:26

Have the same problem, use ''

var var_external =  '<?php echo $var_external; ?>';

Seems java script crash if php echo text and not crash if php echo int.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.