Speeedfire
(Speeedfire)
1
How to add php variable to js?
example:
//php
$baseurl = "http://example.com";
//js
$(document).ready(function(){
$.ajax({
url: baseurl+"/example",
success: function(data) {
$('#example').html(data);
}
});
});
Are you serious?
url: '<?= $baseurl ?>' +"/example",
Speeedfire
(Speeedfire)
3
The js is a another js file.
Ok, that makes sense.
- global config variable
<script>baseurl = '<?= $baseurl ?>'</script>
<script src="... external file ..."></script>
- constructor
var obj = new YourClass({
baseurl: '<?= $baseurl ?>'
....
});
where YourClass is defined in external file.
- …
There are many ways, it depends on your external js file.