Initial definitions
Good practice
Good practices are bad. Doing the right thing for every situation is what should be done.
There are cases where one option is better than the other, just analyzing the concrete case to say. Anyone who says it is good to do this or that without looking at the concrete case is speculating and even rendering a disservice.
Gambiarra
Gambiarra is to do what goes against what the requirements require. Of course we are talking about realistic and not invented requirements.
Any language
Opening yourself up to any language is even more widespread. It is more difficult to say that one is better than another. Obviously the AJAX solution only works with Javascript, and this type of solution is only common using web.
If to use this technique with SQL there is already a greater chance of being valid.
With HTML the default is to do just that.
Separation of responsibilities
In fact the ideal is that the JS is purer, preferably not go through the generation of PHP, ie be in separate file and be static. It is a bad practice to mix data with code, JS is essentially code. But it’s only really bad if there’s another better way to do it. AJAX is not a universal solution. Just because it exists does not mean it should always be used.
Examples of Sopt
Of course, the existing questions on Sopt and other sites are usually not good references to how not to do something. There are a minority of cases that can be used as a good reference.
It’s okay to do this?
If it’s okay to do this, it’s okay to do it, as long as you have a justification to do it, the problem is to do it because everyone is doing it.
It’s a problem to take a dessert recipe filled with sugar and serve it on a diabetic date. But what’s wrong with making sweet, tasty desserts at a confectioners' meeting?
You have to understand the motivations, you have to know why you are doing it. Then everything is valid and correct.
Putting the result of a PHP code as part of what will be inserted into the JS code that will be sent to the browser is absolutely normal. It should only not be used if it has no sense in doing. I understand that the example of the question is only illustrative, but it is a case where there is no need to do this.
Misspelled Javascript
The biggest problem of the code of the question cited (in the comments) is not PHP, it is JS that is not parameterized. You are using JS as a code template and not as a normal function. If the JS is well written and has a function that receives parameters, if you send the data by HTML and only call the JS function in it, it is actually a better way. Isolates JS, HTML code, data.
In fact the cited example would be better written in another way.
AJAX
AJAX is just a solution for sending data outside of HTML as well. It might actually be a good option in some cases. Note that if JS is right, it makes little difference whether the data came from HTML or AJAX request. If AJAX cannot or should not be used in the specific case, it is not solution.
Completion
Overall I wouldn’t do this in most situations I encountered. I would well separate the HTML from the JS, but I would never say it is wrong to do in any case.
Bacco gave an example in the comments. I think instead of putting in HTML some data can put in an auxiliary JS and call the parameterized JS. Let’s say that this would be an AJAX without AJAX :) It can be useful not to pollute HTML with data that is not needed in it. But note that there will still be a clear separation of responsibilities between what is given in JS and what is code.
Immediately the readability of the code (mainly PHP) is impaired.
– Pedro Teles
What is RTA????
– rray
Alternative Technical Resource, alias Gambiarra. Example: http://answall.com/questions/109680/grafico-em-barras-char-js
– Diego Souza
The questions where you see this are closed? rs
– rray
Rsrs do not know if I understand your thinking, but usually they are not closed.
– Diego Souza
For me the ugliest gambiarra there is to use tag script within PHP with console..
– Bacco
What is the alternative @Bacco?
– durtto
That example didn’t work.
– durtto
@durtto log in by PHP itself, show right on screen to anyone who has a developer cookie, show as html comment, access a page from outside to register the error, there are a thousand ways. depends on the case. Nor would it fit in the comment how much alternative you can do. Generally calling <script> PHP is already a code that stinks me.
– Bacco
As for what was asked, I find it too broad, because there are cases and cases. For example, if you are generating a JS map script using PHP to merge coordinates, it is all right. Depending on the case it can be great, depending on the case it can be very bad to merge. If you know what you’re doing, you can merge CSS, JS, SQL and HTML all together in the same PHP that looks beautiful. If you don’t know, it gets disgusting.
– Bacco
Got it @Bacco. I read everyone’s opinion, including the answer below. For me you can close.
– Diego Souza
It seems to be duplicate yes. I actually wanted to know if it’s correct. I saw that there is nothing wrong with doing this, however, it would be good to follow the language pattern. I would not do it anymore.
– Diego Souza
@Zoom I closed, but qq thing leave a comment here if you need to review something.
– Bacco
If the reply of the bigown served, it would be nice to close the post with an Accept not to be open on the list (soon delete the comment)
– Bacco
Did the answer resolve what was in doubt? Do you need something else to be improved? Do you think it is possible to accept it now?
– Maniero