Can I have Javascript write PHP?

Asked

Viewed 823 times

14

  • For what purpose? The question is too broad.

  • I need to make javascript write a php variable

  • 4

    print('php') :P

  • 1

    PHP is processed on the server side, I do not know if this purpose is possible (or even useful), and it is possible to make both "talk" through ajax. Take a look at ajax.

  • @Renan can then print('<? php $variavel=' . value . '?>'); ?

  • See the @bigown response.

  • 1

    Not

  • You can create a doctype file of the PHP type in javascript and then run it via javascript. But that’s a tremendous babaquisse. https://robsonmagno.wordpress.com/2012/03/06/ler-gravar-e-criar-txt-com-javascript/

  • because it only works in IE garbage.

  • You can write javascript files: https://nodejs.org/docs/latest/api/fs.html

Show 5 more comments

3 answers

15


Essentially not.

And many programmers don’t understand this. They don’t understand what I said in that question. PHP just generates text that by coincidence can be a JS code, PHP doesn’t even know what that is.

A JS inside a PHP code will not run on the server, it is just a text, for PHP that is not a code.

JS will run on the client, probably a browser and has no direct contact with PHP. The contact between the JS code on the client and the PHP code on the server will be through communication through the HTTP protocol. That is, the JS can send data to the server that will pass to the PHP code to decide what to do with it. Only this.

Simplified example:

var req = new XMLHttpRequest();
req.onreadystatechange = function() {
    if (req.readyState == 4 && req.status == 200) {
       // faz alguma coisa;
    }
    req.open("GET", "pagina.php", true);
    req.send();
}

I put in the Github for future reference.

Of course you can use the JS on the server to help PHP on some task and even generate a PHP code for the server to run, but it’s something separate and not what people usually do, probably because it’s too complex and gain less.

Within the "normal" use of PHP, you cannot.

  • so I can’t do this 'print('<? php $variavel=' . value . '?>'); ' ?

  • 4

    Power, can, but won’t do what you’re thinking.

  • then I can create a cookie with javascript and make php read this cookie ?

  • Do you want to exchange information between JS and PHP using cookies? I believe it is better via ajax.

  • 1

    You can communicate it via the HTTP protocol. O cookie exists only in the client, and it has to inform the server of its existence. The browser does this by the protocol. It is also possible to send via JS.

  • The perfect one I’ll try using ajax

Show 1 more comment

14

You cannot because javascript works on the client side of the http cycle and php works on the server side, so the code generated by javascript will never run on the server, it will only be a common string/text containing php instructions.

If you need to make javascript work with some php feature use ajax.

3

The answer is yes, but not for the purpose you want. Using the Node.js it is possible to write files:

Obs: all languages write files. But not all of them work together.

fs.writeFile('message.txt', 'Olá Mundo!', function (err) {
  if (err) throw err;
     console.log('Nada foi salvo!');
});

No . htaccess, just put this and your txt files become php:

AddType application/x-httpd-php .txt

Here is more information about: https://nodejs.org/docs/latest/api/fs.html

Here is another practical example that it is possible to write files and if you save a txt with the extension ". php", it becomes a PHP file:

Sample demo

Github Filesaver

Browser other questions tagged

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