-1

In our webpage, we need to access to a PHP variable inside a javascript code. Specifically, we need to pass a parameter that is a URL defined by a PHP variable called $link_es

I've tried doing this

var r_obj = {
"Company": { "CompanyId": xxxxxx },
"RatingboxId": xxxxx,
"ProductCode": encodeURIComponent($link_es),

where $link_es is the URL stored in a variable that we need to pass as parameter.

How could I pass that parameter? That is, how could I convert a PHP variable into a Javascript one? Thank you.

PD: I'm using Smarty PHP so there could be some problems there.

user6375350
  • 95
  • 1
  • 8

9 Answers9

1

If the script is inside a tpl file, it's as easy as assigning it to a javascript variable:

<script>
var link_es = '{$link_es}';
//do whatever you want with it
alert(link_es);    
</script>

make sure that the javascript code isn't between {literal} tags, though

Borgtex
  • 3,235
  • 1
  • 19
  • 28
  • What to do if the javascript code is within `{literal}` ? I am facing same problem. – Jaykumar Patil Nov 09 '16 at 12:37
  • just define the variable before the literal block or close the literal block with {/literal}, define the variable and then open {literal} again. Also, if you're using smarty 3 you probably don't even need to use literal, as long as every opening bracket ("{") in the javascript code has a space after it. – Borgtex Nov 09 '16 at 14:43
  • This code will not work for object structures. – nikksan Apr 16 '18 at 12:08
0

Try to make a pre defined javascript variable whose value is set by PHP

<script>
var link_es = "<?php echo $link_es ?>";
</script>
Deben Oldert
  • 114
  • 1
  • 11
0

Try

"ProductCode": encodeURIComponent(<?= $link_es ?>)
Prem Raj
  • 849
  • 4
  • 15
0

One way would be to use something like this in your PHP file:

<script>
var myVar = <?php echo $link_es; ?>;
</script>
RST
  • 3,899
  • 2
  • 20
  • 33
0

In this cases you can work as follow:

Add following code in your PHP file

<script>
     var jsVariable = <?php echo $phpVariable ?>;
</script>

And in js file can simply access to variable as follow:

console.log(jsVariable);

If you want to pass an array first should json_encode that like follow:

<script>
     var jsVariable = <?php echo json_encode($phpVariable) ?>;
</script>

in this case you have an object in javascript which and trace that with console.log

Majid Abbasi
  • 1,531
  • 3
  • 12
  • 22
0

Case 1 If you writing your javascript on Php page then you can use pass it like

"ProductCode": encodeURIComponent('<?= $link_es ?>'),

Case 2 if you have separate javascript file then you have to define global variable in your php file then you can access this in your js file.

<script>
     var jsVariable = <?php echo json_encode($link_es) ?>;
</script>

In Case 2, I personally used to store this value in hidden field with some id then i use it in js file

<input type="hidden" id="link_es" value="<?= $link_es ?>" />

//then in js file access like this

"ProductCode": encodeURIComponent($("#link_es").val()),
Aabir Hussain
  • 1,161
  • 1
  • 11
  • 29
0

I myself have finally found a solution. It consists of the following:

First of all, we include a div tag like the below

<div id='linkespanol' style="display: none;">
{$link_es}
</div>

Later on, inside the script, we can access to the full URL like that

var a = window.location.href.split("/")[0];
var b = window.location.href.split("/")[2];
var c = a+"//"+b;
var linkidioma = c+document.getElementById('linkespanol').textContent.trim();

Variable $link_es doesn't contain full URL, but /es/productCode.html so in order to generate full URL I use window.location.href to get what I want from the current URL and finally concatenate both. I use trim to get rid of some blank spaces.

It's possibly a quite bad solution but works. Possibly, without using Smarty, there could be a simpler way.

user6375350
  • 95
  • 1
  • 8
-1

Just add this <?php echo $link_es; ?> That will echo out the variable into the javascript.

Blinkydamo
  • 1,582
  • 9
  • 20
-1

None of the suggested answers helped, I ended up doing this.

var data= JSON.parse('{$data|@json_encode|@addslashes}');
nikksan
  • 3,341
  • 3
  • 22
  • 27