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

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

有没有办法做到这一点?


智慧大石
浏览 140回答 1
1回答

江户川乱折腾

已在此处回答:-&nbsp;单击按钮运行 php 函数我这边的另一个例子:-如何在单击按钮时调用 PHP 函数?<h1 style="color:green;">&nbsp;&nbsp; &nbsp; GeeksforGeeks&nbsp;</h1>&nbsp;<h4>&nbsp;&nbsp; &nbsp; How to call PHP function&nbsp;&nbsp; &nbsp; on the click of a Button ?&nbsp;</h4>&nbsp;<?php&nbsp; &nbsp; if(array_key_exists('button1', $_POST)) {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; button1();&nbsp;&nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; else if(array_key_exists('button2', $_POST)) {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; button2();&nbsp;&nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; function button1() {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; echo "This is Button1 that is selected";&nbsp;&nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; function button2() {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; echo "This is Button2 that is selected";&nbsp;&nbsp; &nbsp; }&nbsp;?>&nbsp;<form method="post">&nbsp;&nbsp; &nbsp; <input type="submit" name="button1"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; class="button" value="Button1" />&nbsp;&nbsp; &nbsp; <input type="submit" name="button2"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; class="button" value="Button2" />&nbsp;</form>&nbsp;来源:- https://www.geeksforgeeks.org/how-to-call-php-function-on-the-click-of-a-button/
打开App,查看更多内容
随时随地看视频慕课网APP