0

Maybe for someone it's easy to do, but I can not write well this code:

$("<div id='conferm'></div>")
  .appendTo("#contact")
  .html("<span>Your products is Add!<br>check your cart <a href='<?= $config->urls->root ?>cart'>here</a> or keep to shopping</span>")

The problem is the link not work, because the php $config->urls->root doesn't print the URL but just print it like a text.

If I add this code inside my php page works well, but inside a js file not works.

How I can write it for let the link work well?

Thank you!

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Marco Romano
  • 272
  • 1
  • 14
  • 3
    You can't as PHP isn't interpreted within a file with a .js extension. If you want to get the PHP value, write it to the page somewhere, as a `data` attribute on the `body` for example, then read that out in your JS file – Rory McCrossan Feb 22 '18 at 14:30
  • You would have to do an inline script within a PHP file, and then execute the PHP inside that. – Hunter Feb 22 '18 at 14:31
  • @RoryMcCrossan I wrote an answer not knowing you gave hints about the problem already, I'll leave it, it might be useful :) – animake Feb 22 '18 at 14:37
  • instead of using php, javascript's `window.location.host` gives root url and `window.location.protocol` gives protocol (http or https) you can use that if needed – Haseeb A Feb 22 '18 at 14:50
  • or this may help https://stackoverflow.com/questions/5559578/having-links-relative-to-root#5559597 – Haseeb A Feb 22 '18 at 14:52
  • Thank you @RoryMcCrossan, I suspected it was so. Thank you! – Marco Romano Feb 22 '18 at 16:35

2 Answers2

0
<?php 
     echo '<script> var rooturl = ' . $config->urls->root . ';</script>' 
?>

And then change this to:

$("<div id='conferm'></div>")
      .appendTo("#contact")
      .html("<span>Your products is Add!<br>check your cart <a href='" +rooturl+ "cart'>here</a> or keep to shopping</span>");

Take into consideration the load order so that your JS file knows your rooturl variable. Your PHP must print the echo code on your html document before you process it on your JS file.

animake
  • 309
  • 2
  • 8
  • However since we don't know what else is in his .js file (like a document.ready possibly), this may not work if the .js include is before the echo'd script block in the html. So care is to be taken with this method about 'load order'. – IncredibleHat Feb 22 '18 at 14:37
  • You are absolutely right @IncredibleHat. I guess he might be aware of that but yah.. I'll update the answer. – animake Feb 22 '18 at 14:39
  • Thanks @animake and IncredibleHat this work so well!! Yes sure, I have add the main.js in the footer. Thank you Again!! – Marco Romano Feb 22 '18 at 16:45
0

You can not write PHP code in .js files, because they are made for javascript only. But, you could do the following:

Make ajax function in your .js file, that will get you proper value for your href attribute

var link_href = "";
$.ajax({
  data: "",
  url: "my_php_file.php",
  type: "GET",
  success: function(data){
   link_href = data;
    //data is what your .php file echos
    //in your example you want to make sure your php files echos only href attribute you need for your <a> tag
  }
});
var a = document.getElementById("a_link");
a.setAttribute("href", link_href);
lyyka
  • 450
  • 3
  • 11