Is it correct to concatenate PHP in JavaScript?

8

I often use this method, but I do not know if it is appropriate, to redeem the id in JS I use echo php

<script type="text/javascript" src ="js/jquery.js"></ script>
<script type ="text/javascript">

$("document").ready(function(){
    $('.artigo-<?=$artigo['artigo_id']?>').click(function(){
        /* executa funçao */
     });
});
</ script>

<? php
$ artigo[] = array (); 
$ artigo['artigo_id'] = 1;
?>

<a class="artigo-<? = $artigo['artigo_id']?> " href =" javascript: void (0); ">executar</a>
    
asked by anonymous 09.01.2016 / 15:24

2 answers

9

It's okay to do this. Everything between <?php ?> and <?= ?> will be interpreted by PHP, the rest is just any text that will be sent to the HTTP server and consequently to the browser without modification.

Just remember that the code you are writing is PHP. It is possible to merge free text within PHP code into a technique known as templating . Note that this is a text that PHP has no knowledge of, can have whatever you want there. Of course it does not make sense for anything, it makes sense to place HTML and quite possibly CSS and / or JS.

It is not usually good to mix CSS and JS in HTML unless in very rare cases very well thought out and very tightly. This is a case where you should probably have most of the JS / jQuery in another file working with parameter to get the like artigo_id . It would not even be absurd to have in HTML the call to the function that does this.

Of course I hope this code is just an example. It does nothing useful in the way it was placed. It would be much simpler.

See more .

    
09.01.2016 / 15:33
1

At first it is good to clarify that "Concatenating PHP in JS" is perhaps not the best description of what happens there, I believe the correct term would be something like "Dynamically generating JS with PHP", what happens is that you creates parts of javascript by printing (echo) PHP content.

Answering the question: depending on your case I would say that it is the most appropriate option, yes. If you have a small JS, say something like your example of about 20 lines or less, and it needs to manipulate data that is server-side (php), give an echo and pick up the content you need from PHP is simply the way simpler, if it works and is readable code you do not have to complain.

If your JS has a reasonable size the situation already changes, having a mega block of javascript in the middle of PHP code is something that I consider dreadful, it completely disagrees with the code around, it is difficult to read and it is something that sharply breaks separation of responsibilities. In this case I recommend putting the JS in place, a separate .js file, and for you to have access to the phpnian values (heh that unfortunate word) you need there are three options:

  • Ajax, your javascript makes a request to the server asking for the values it needs. It is a "pure" solution since you do not mix php and javascript directly, however it is the most bloated since you will have to have an AJAX block in js, a php endpoint ("url that ajax will call") and a request extra that the client will have to do after loading the page.
  • In php echo the data that js needs as the value of an input hidden, eg echo '<input id="mypreciousdata" type="hidden" value="' . $var . '" />'; and in js you do something like console.log($("#mypreciousdata").val()); . This is also a "pure" solution in terms of code separation, does not generate overhead as ajax and is fairly simple.
  • In php generate a JS that has the data stored in some global object, ex: echo '<script>var MeuApp = {data1: "' . $var . '", data2: "' . $var2 . '"};</script> and in your JS simply access that variable console.log(MeuApp.data1); . This is usually my most used strategy because I consider pragmatic. Although it mixes js and php code to insert the data into the object will be just a few lines of code in it, being the bulk of your js that will use the data in a separate file.
  • 22.01.2016 / 16:16