从 <button onclick=""> 执行 PHP 函数

我想在单击 HTML 按钮时执行 PHP 脚本。我知道,我不能这样做: <button onclick="myPhpFunction("testString")">Button</button> 或类似的事情。我的目标是,单击此按钮时在我的服务器上执行一些命令。注意:我的 php 函数需要来自 JS/HTML 的变量。

有没有办法做到这一点?


米脂
浏览 64回答 1
1回答

慕少森

如何通过单击按钮调用 PHP 函数?<h1 style="color:green;">     GeeksforGeeks </h1> <h4>     How to call PHP function     on the click of a Button ? </h4> <?php    if(array_key_exists('button1', $_POST)) {         button1();     }     else if(array_key_exists('button2', $_POST)) {         button2();     }     function button1() {         echo "This is Button1 that is selected";     }     function button2() {         echo "This is Button2 that is selected";     } ?> <form method="post">     <input type="submit" name="button1"            class="button" value="Button1" />     <input type="submit" name="button2"            class="button" value="Button2" /> </form> 
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Html5