Pass variable PHP to "file" javascript [duplicate]

-3

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.

    
asked by anonymous 03.12.2017 / 23:38

2 answers

3

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;
    });
});
    
04.12.2017 / 01:32
1

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>     

04.12.2017 / 00:22