PHP does not know Javascript
PHP cannot directly call a Javascript function because PHP is interpreted on the server side.
PHP does not see a code there, it simply copies the text character by character and sends it to the user’s browser.
The browser knows Javascript
When the browser reads the received data already in HTML, it starts to interpret this HTML and assemble the elements on the screen.
When finding a tag <script>
nevegador stops what it is doing and executes what it has in this script.
Note that at this point, the browser is running the script on the user’s computer, while PHP (which has possibly finished running) was running on the server.
Execute code on page loading
Then, for all purpose, the code quoted in the question will run in the browser exactly at the time the browser receives it from the server. No explicit call is required.
<script language="javascript" type="text/javascript">
//todo código aqui será executado
</script>
Performing functions
However, if you have a declared function, the snippet is interpreted and the function is created, but it is not automatically invoked:
<script language="javascript" type="text/javascript">
function f_mostra() {
alert("Entrei");
}
</script>
However, just add a later call to the function declaration to run it:
<script language="javascript" type="text/javascript">
function f_mostra() {
alert("Entrei");
}
f_mostra();
</script>
Or else:
<script language="javascript" type="text/javascript">
function f_mostra() {
alert("Entrei");
}
</script>
<!-- ... -->
<script language="javascript" type="text/javascript">
f_mostra();
</script>
Waiting for the page to be ready
The problem is that it is not always desirable to run Javascript while the page is being loaded, after all the browser may not have finished assembling all the HTML.
It is then possible to capture the event that says if the page is fully loaded:
window.onload = function() {
//executado quando tudo na página está carregado
};
Everything I said above is not exactly true ;)
There are techniques and technologies that actually make it possible for PHP (or any other language) to perform a Javascript function when the user has an open page, without a new request.
Today with HTML 5, we have the technology Websockets which enables persistent communication between client (Javascript) and server (PHP). However, I will not go into detail as it is a more complex subject.
Explain your goal better, there might be another way to do this. If I understand what you want, you can’t do it directly. Maybe it’s even possible to do something gambiarra with some engine J, but I’d rather find another solution.
– Maniero
You can run javascript on the server side using Node.js for example. Maybe this is a viable path to your problem.
– dap.tci
You can place the above HTML snippet into a block on the PHP page within an IF conditional, for example, and render it according to your requirements.
– OnoSendai
But in my case @Onosendai, does my excerpt above run in php? Because honestly for the short time I’m in this language, I haven’t seen this code run in a php block!
– Alexandre
@Dap.Tci, what would this method look like? I’m sorry, but I don’t know this!
– Alexandre
Now that you have added the code snippet you want to run, I believe that Node.js is not the best option. Why don’t you make this loop with these treatments in PHP and then pass these variables to javascript (
objp
andobjs
) ready to be used?– dap.tci
@Alexandre The JS code will not run in PHP - only its output will be controlled by PHP. So: <?php if ($show) { ? >HTML content<? php } ? >. I don’t know if the syntax is feasible (I’m not an expert on PHP), but the flow control would be like this.
– OnoSendai
Is there any way to adapt this excerpt in PHP? In this case I only add later in JS'
– Alexandre
In addition to explaining what you want to do with the code, explain what is happening in the interface, what the page does in response to what user action. . . . Please, instead of clarifying things here in the comments, click on the link [Edit] below the question and add in it the new information.
– brasofilo
PHP -> Server side, JS -> Client side. Need to WELL understand the difference!
– Peter
Alexandre, in my answer to your previous question I had already asked for more details about what you are trying to do: what the arrays contain (in PHP), and what the merged array should contain. Everything indicates that you should solve in PHP and pass the array ready pro JS, but no one will be able to help you without these details. Please [Edit] your question to include them. Thank you.
– bfavaretto