0

Below is my JavaScript variable and I want to echo it using php on button click.

    <html>
    <body>
    <button onclick="runphp()">Echo Hi</button>
    <script>
    function runphp() {
    var a = "Hi";
    //echo a with php
    }
    </script>
    </body>
    </html>
  • PHP is server-side, JS is client-side. You can't run PHP in JS. Use AJAX – brombeer Apr 11 '21 at 10:21
  • Yes I need help on that –  Apr 11 '21 at 10:22
  • Since you tagged this with `jquery`, here's [jQuery's .ajax documentation](https://api.jquery.com/Jquery.ajax/) – brombeer Apr 11 '21 at 10:24
  • I want to use jquery but I can't find how to take a javascript variable in php script –  Apr 11 '21 at 10:26
  • 2
    *take a javascript variable in php* - you don't. You take a URL parameter of POST variable. Don't think of trying to run them "together" - think of them as separate. Your JS sends some data using its methods and your php receives the data using *its* methods - which are completely different. – freedomn-m Apr 11 '21 at 10:33
  • You. Can. Not. That's why I posted the .ajax documentation link. There are examples on that page posting to a `.php` file. – brombeer Apr 11 '21 at 10:34
  • To not repeat above comments... You can mimic an echo in javascript: [Is there a php echo/print equivalent in javascript](https://stackoverflow.com/questions/13633085/is-there-a-php-echo-print-equivalent-in-javascript) – Ivan86 Apr 11 '21 at 10:54

0 Answers0