0

I have a php varaiable that I need to pass into a link in a javascript function

//In the config
<?php
    define('THE_VAR', 'test');
?>


//On the page
<?php
    $the_var = THE_VAR;
?>

<script>
    (function()){
        link = "test.site"+<?php echo json_encode($the_var); ?>+"/testing.js"
    })();
</script>

I need the link in the javascript function to read.

test.site/test/testing.js

2 Answers 2

2

Instead of: link = "test.site"+<?php echo json_encode($the_var); ?>+"/testing.js"

Use: link = "test.site/<?= $the_var; ?>/testing.js";

2
  • that just outputs "test.site/'+"test"+'/testing.js";
    – ttmt
    Mar 20, 2015 at 13:10
  • True, I made a mistake! Try it now, I edited my post. Mar 20, 2015 at 13:15
-1

Use Ajax for passing the data from php into the javascript.

http://www.w3schools.com/ajax/ajax_php.asp

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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