-5

How to pass a variable from php to javascript if the script is on another server? It is necessary that the value from select is passed to the script, to the "symbol": parameter. It should all be in one php file Code:

echo '
<!-- TradingView Widget BEGIN -->
<div class="tradingview-widget-container">
    <div class="tradingview-widget-container__widget"></div>
    <script type="text/javascript" src="https://s3.tradingview.com/external-embedding/embed-widget-technical-analysis.js" async>
    var pair = "FX:GBPUSD";
    {
      "interval": "1m",
      "width": 425,
      "isTransparent": false,
      "height": 450,
      "symbol": pair,
      "showIntervalTabs": true,
      "locale": "ru",
      "colorTheme": "light",
      "largeChartUrl": "https://example.com"
    }
    </script>
</div>
<!-- TradingView Widget END -->
';

?>

I tried various options, but nothing works until explicitly specified in the script, for example: "symbol":"FX:GBPUSD"

JoSSte
  • 2,953
  • 6
  • 34
  • 54
  • `var data = = $myPhpvar ?>` – Chris G Feb 10 '23 at 12:34
  • This answer to your question? https://stackoverflow.com/questions/23740548/how-do-i-pass-variables-and-data-from-php-to-javascript – Álvaro Feb 10 '23 at 12:35
  • _"if the script is on another server?"_ - that has absolutely nothing to do with your issue here. The script resource itself might be loaded from a different server - but the value that you want to set dynamically here, `"symbol": pair,`, is within the code you output on your own page. – CBroe Feb 10 '23 at 12:35
  • Read https://stackoverflow.com/a/23740549/1725871 - much better answer than mine... – JoSSte Feb 10 '23 at 12:38
  • It doesn't work – Oleg Pavlenko Feb 10 '23 at 13:04

1 Answers1

0

You can do it in several ways:

  1. Generate a json file using php with your values and fetch that using javascript, parse it and use it in javascript.
  2. echo the php value into your javascript, which is ugly but simpler:
<?php
  $somevar="hello from php";
?>
<html>
<body>
  <script>
    let myphpvar = '<?=$somevar?>';
    alert(myphpvar);
  </script>
</body>
</html>
JoSSte
  • 2,953
  • 6
  • 34
  • 54
  • 1
    This question gets asked literally hundreds if not thousands of times per day on stack overflow. Please don't answer it yet again. Just flag it as a duplicate. – Jared Smith Feb 10 '23 at 12:34
  • 1
    Isn't better an AJAX call? – Álvaro Feb 10 '23 at 12:34
  • @ÁlvaroPérezDíaz that is exactly what option #1 is – JoSSte Feb 10 '23 at 12:35
  • Oh yeah, sorry, I didn't read well – Álvaro Feb 10 '23 at 12:36
  • 1
    @ÁlvaroPérezDíaz no almost certainly not. You already have the data to show the user and can directly inject it into the page without the network round-trip. The only time AJAX makes sense is when the data is so large parsing it would cause the browser to delay FCP or TTI. – Jared Smith Feb 10 '23 at 12:36
  • Yeah, I know, but using PHP to render a script tag wouldn't make easier a injection? – Álvaro Feb 10 '23 at 12:37
  • It doesn't work – Oleg Pavlenko Feb 10 '23 at 13:04
  • There are no errors in the validator, but the fact is that my code does not work if it is placed on the site. I can see without a validator that there are no errors, but my code does not work and the widget from TradingView is not displayed. Try to place my code on the test site yourself and execute it. Here is the source code from TradingView in which I want to change this parameter: "symbol":"FX:...?" https://ru.tradingview.com/widget/technical-analysis /?widget_symbol=FX%3AEURUSD – Oleg Pavlenko Feb 11 '23 at 07:26
  • Solution: Outside of the script, we create a variable with the parameter name and value included in it completely and then insert it into the script, then it works... $pair="GBPUSD"; $param='"symbol": "FX:' . $pair . '"'; echo ' '; – Oleg Pavlenko Feb 11 '23 at 07:36