php按钮点击显示_在html按钮上单击运行PHP功能

每当您通过HTTP请求(即GET,POST,PUT)访问它时,都会运行PHP文件.

您可以使用JQuery / Ajax在按钮点击上发送请求,甚至只需更改浏览器的URL即可导航到PHP地址.

根据POST / GET中发送的数据,您可以有一个运行不同功能的switch语句.

通过GET指定功能

所以在PHP方面你可以有这样的东西:

//see http://PHP.net/manual/en/function.call-user-func-array.PHP how to use extensively

if(isset($_GET['runFunction']) && function_exists($_GET['runFunction']))

call_user_func($_GET['runFunction']);

else

echo "Function not found or wrong input";

function test()

{

echo("test");

}

function hello()

{

echo("hello");

}

?>

您可以使用地址栏做最简单的获取请求作为测试:

http://127.0.0.1/test.PHP?runFunction=hellodddddd

结果是:

Function not found or wrong input

http://127.0.0.1/test.PHP?runFunction=hello

结果是:

hello

发送数据

通过JQuery获取请求

$.get("test.cgi",{ name: "John"})

.done(function(data) {

alert("Data Loaded: " + data);

});

POST请求通过JQuery

$.post("test.PHP",{ name: "John"} );

通过Javascript位置获取请求

value="insert button text here"

onClick="self.location='Your_URL_here.PHP?name=hello'">

阅读数据(PHP)

有用的链接

要实现这个功能,你需要使用一些编程语言和技术,例如PHP、SQL和HTML。下面是一个简单的例子: 1. 首先,你需要创建一个HTML页面,在页面中添加一个按钮和一个DIV元素,用于显示数据库中的数据。例如: ``` <!DOCTYPE html> <html> <head> <title>显示数据库数据</title> </head> <body> <button onclick="showData()">显示数据</button> <div id="data"></div> </body> </html> ``` 2. 接下来,你需要编写一个JavaScript函数来处理按钮单击事件,并从数据库中获取数据。例如: ``` function showData() { var xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { document.getElementById("data").innerHTML = this.responseText; } }; xmlhttp.open("GET", "getdata.php", true); xmlhttp.send(); } ``` 3. 然后,你需要创建一个PHP文件(例如getdata.php),用于从数据库中获取数据并返回给JavaScript函数。在该文件中,你可以使用SQL查询来检索数据。例如: ``` <?php $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "myDB"; // 创建连接 $conn = new mysqli($servername, $username, $password, $dbname); // 检查连接是否成功 if ($conn->connect_error) { die("连接失败: " . $conn->connect_error); } // 执行SQL查询 $sql = "SELECT * FROM myTable"; $result = $conn->query($sql); // 输出数据到HTML页面 if ($result->num_rows > 0) { while($row = $result->fetch_assoc()) { echo "id: " . $row["id"]. " - Name: " . $row["name"]. "<br>"; } } else { echo "0 结果"; } $conn->close(); ?> ``` 4. 最后,你需要配置Web服务器以运行PHP文件,并将HTML文件和PHP文件放置在服务器上。当你单击按钮时,JavaScript函数将从getdata.php文件中获取数据,并在HTML页面上显示它们。 请注意,这只是一个简单的示例,实际上你需要更多的代码和技术来处理数据的格式化和安全性等问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值