0

hi I write a php file that contains two methods :

1)

function restart(){ echo('restart') };

2)

function shutdown(){echo ('shutdown')};

then I include the index.php file to the foo.php and when I want to call index.php methods in an eventHandler in foo.php it doesn't work like this:

    <?php
include ('index.php');

?>


<html>
    <head>
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        
    <button id='res'>restart</button>
    <button id='sht'>shutdown</button>
    <script>
        document.getElementById('res').addEventListener('click',function(){
            console.log('restarted');
            <?php

           restart();
           ?>
        })
    </script>
    </body>
</html>

output: nothing and no errors


and when I remove the php code inside the eventHandler it works! like this:


  <?php
    include ('index.php');

?>


<html>
    <head>
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        
    <button id='res'>restart</button>
    <button id='sht'>shutdown</button>
    <script>
        document.getElementById('res').addEventListener('click',function(){
            console.log('restarted');
           
        })
    </script>
    </body>
</html>

output: restarted

WHAT IS THE PROBLEM?

AndrewL64
  • 15,794
  • 8
  • 47
  • 79
  • 2
    You need a semicolon after the `echo` statements - at the moment it's outside the function (after the `}`). – Nigel Ren Jun 06 '21 at 07:19
  • 1
    Turn on error logging to the screen or to a file and see what error(s) PHP is throwing. – obe Jun 06 '21 at 07:22
  • You can using ajax request to server instead of call php code in javascript code. See https://stackoverflow.com/questions/15757750/how-can-i-call-php-functions-by-javascript – ParisaN Jun 06 '21 at 07:50
  • Does this answer your question? [What is the difference between client-side and server-side programming?](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) – AD7six Jun 06 '21 at 10:32

2 Answers2

1

The PHP code is being run when the file is 'rendered', so the function call to restart, even though it is within the JavaScript event listener, is being run on render. If you check the source (after fixing the missing semicolon), it should show restart in place of the PHP code.

You cannot call PHP code within JavaScript, as JavaScript is client side and PHP is server side.

Will Walsh
  • 1,799
  • 1
  • 6
  • 15
-1

You have to use console.log in echo statement as well.. follow the below example. Assuming your index.php file code is already been added on top to explain you better.

<?php 

function restart(){ echo (" console.log('restarted')"); }

function shutdown(){echo (" console.log('shutdown')"); }

?>

<html>
    <head>
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        
    <button id='res'>restart</button>
    <button id='sht'>shutdown</button>
    <script>
        document.getElementById('res').addEventListener('click',function(){
           //console.log('restarted');
            <?php
           restart();
           ?>
        })
    </script>
    </body>
</html>
Muhammad Asif
  • 456
  • 4
  • 10
  • I think this answer is probably missing the point (that the OP expects the php restart function to be called when clicking the button in the web) and is indirectly pointing out that the output of the php function, when embedded in a script tag, must be valid js. But no php function should be called like that, the problem/misunderstanding is larger than that. As written, the question’s code is missing an xhr request. – AD7six Jun 06 '21 at 10:28
  • The question is simple the user needs to call php function based on button click , so I did that according. If you have a time to test than this code does fulfill the requirement. Let the actual user respond as what he says about this. – Muhammad Asif Jun 06 '21 at 10:33
  • That is not true, please consider the “restart” function actually doing something (e.g. replace it with `exit;`) and when it would do it - The php function is called on render. Please see the duplicate question I’ve linked to. – AD7six Jun 06 '21 at 10:34
  • Its not, just remove console.log('restarted') from JavaScript code and then test, when you will click the restart button than only that function will be called, check in console. – Muhammad Asif Jun 06 '21 at 10:36
  • I have commented console.log from JavaScript, now test. – Muhammad Asif Jun 06 '21 at 10:36