我想运行PHP脚本onclick事件的按钮标签是可能的


I want to run PHP script onclick event of button tag is it possible

这是我想要运行的代码。。。计算是用PHP进行的,我希望它在button标记的onclick事件上运行。

 <button onclick = "myFunction()"> Click here to calculate</button>
  <script> 
  function myFunction()
  { <?php $x = 100; $y = 2;
   echo "This is multiplication of x and y ="." ". $x*$y."</br>"; 
   echo "This is addition of x and y ="." ".($x + $y)."</br>";
   echo "This is subtraction of x and y ="." ".($x - $y)."</br>"; 
   echo "This is division of x and y = "." " .$x/$y."</br>";
   echo "This is remainder of x and y ="." ". $x % $y."</br>"; ?>
 }
 myFunction();
 </script>

这可能吗?请帮帮我。。

当您寻求帮助时,请检查以下内容:-

1:-创建一个HTML文件

    <html>
    <script src = "jquery-1.9.1.js"></script>
    <button id ="click_button"> Click here to calculate</button>
    <script>
    $(document).ready(function(){
        $('#click_button').click(function(){
            $.ajax({
                url:'buttonclick.php',
                success: function(Response){
                    alert(Response);
                }
            });
        });
    });
    </script>
     </html> 

2:-创建一个PHP文件(名称为buttonclick.PHP):-

<?php 
  function myFunction()
  { 
   $x = 100; $y = 2;
   echo "This is multiplication of x and y ="." ". $x*$y."</br>"; 
   echo "This is addition of x and y ="." ".($x + $y)."</br>";
   echo "This is subtraction of x and y ="." ".($x - $y)."</br>"; 
   echo "This is division of x and y = "." " .$x/$y."</br>";
   echo "This is remainder of x and y ="." ". $x % $y."</br>";
 }
 return myFunction();
?>

输出:-http://prntscr.com/6l56dq

注意:-文件(html和php)和jquery文件(jquery-1.9.1.js)将在同一工作目录中。这也取决于你想如何表现出回应。我只是给你举了一个容易理解的例子。

您的问题有些混乱。

PHP是一种服务器端脚本语言,因此它是在浏览器上呈现页面之前执行的。

这意味着您的功能是在服务器中执行的,而不是在用户单击时执行的。

如果你想在点击时执行一些计算,你应该使用javascript,因为它是浏览器可以理解的语言。

如果你想使用PHP来计算值,你应该使用AJAX。要做到这一点,您可以使用jquery方法,如$.ajax()$.get()$.post()