I can pass a PHP variable to javascript if it is on the same page, but the problem is that if I throw that code into a JS < in> it does not work.
I can pass a PHP variable to javascript if it is on the same page, but the problem is that if I throw that code into a JS < in> it does not work.
Yes, you can do this as long as javascript does not come from an external source.
PHP Page
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script><scripttype="text/javascript">var param = "<?= $pagina ?>";</script>
<script type="text/javascript" src="js/paginacao.js"></script>
paginacao.js
jQuery(document).ready(function(){
var pagina = param;
jQuery('#btnpaginas').click(function(){
pagina++;
var dados = jQuery( this ).serialize();
jQuery.ajax({
type: "POST",
url: "paginacao.php?pagina="+pagina,
data: dados,
success: function(data)
{
$(".paginacao").html(data);
}
});
return false;
});
});
I do not think there is a way to pass PHP variables to a static external .js
file, since it is done in the actual .php
file that is being executed. p>