Is it possible to run a Javascript function in PHP?

Asked

Viewed 23,564 times

6

I want to run a function of JavaScript without "submitting" it, or when the screen is accessed the php will call the function JavaScript and run what’s in it and then continue the normal code. It’s possible? Obs: The function contains data that only rotate in the JavaScript!

My goal is to run this stretch:

<script language="javascript" type="text/javascript">
    for(i = 0; i < 10; i++){
        if(objp!=''){           
            n_p[i] = 'P' + objp[0];
            objp.splice(0,1);
        }
        else{
            n_p[i] = 'S' + objs[0];
            objs.splice(0,1);
        }   
    }
</script>
//No PHP

For a better understanding of the above section follow Here

  • 1

    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.

  • 1

    You can run javascript on the server side using Node.js for example. Maybe this is a viable path to your problem.

  • 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.

  • 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!

  • @Dap.Tci, what would this method look like? I’m sorry, but I don’t know this!

  • 1

    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 and objs) ready to be used?

  • @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.

  • Is there any way to adapt this excerpt in PHP? In this case I only add later in JS'

  • 1

    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.

  • 1

    PHP -> Server side, JS -> Client side. Need to WELL understand the difference!

  • 1

    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.

Show 6 more comments

2 answers

14


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.

  • 2

    Thanks @utluiz ! Finally someone didn’t ignore my problem and showed me how to solve it!

-3

It’s very simple, just call the Javascript function by echo in PHP.

echo '<script>funcaophp();</script>'
  • 2

    But that’s not calling the JS function with PHP; it’s just writing, with PHP, a text whichever which, by chance, will be interpreted as JS in the browser. They are very different things. The accepted answer deals with this, if you want to read more about.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.