'How to embed php in javascript?

Face problem when writing PHP syntax in javascript syntax.

var id = $("#data-1").val();
var url = '<?= base_url('home/alone/'); ?>.'id'';
console.log(url);

I need to place the id at the end of url. But my code is not working. What is wrong? Thank you in advance.



Solution 1:[1]

use " instead of ' like this:

var id = $("#data-1").val();
var url = "<?= base_url('home/alone/'); ?>" + id;
console.log(url)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Milad Elyasi