点击html按钮运行PHP函数
Run PHP function on html button click
我需要在单击按钮时运行一些PHP函数。我知道这不应该是php的使用,而应该是js来做,但是当用户要求时,我的函数正在从服务器收集数据。具体来说,它获取一些用户数据并将其写入文件,用户应该决定收集哪些数据。
我该怎么做呢?我看到的帖子运行PHP文件上的按钮点击,但我仍然不知道如何使用它。
我正在学习,所以请不要太苛刻
我试过onclick()
和各种各样的东西,但它没有导致任何有用的
无论何时通过HTTP请求访问php文件,无论是GET,POST, PUT,
你可以使用JQuery/Ajax发送一个按钮点击请求,甚至只是改变浏览器的URL导航到php地址。
根据在POST/GET中发送的数据,您可以让switch语句运行不同的函数
通过GET指定函数
您可以使用下面的代码:如何从存储在变量中的字符串调用PHP函数,以及根据发送的数据自动调用适当的函数的switch语句。
在PHP端你可以这样写:
<?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");
}
?>
,你可以做一个最简单的get请求使用地址栏作为测试:
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
发送数据
GET Request by JQuery
见:http://api.jquery.com/jQuery.get/
$.get("test.cgi", { name: "John"})
.done(function(data) {
alert("Data Loaded: " + data);
});
POST Request by JQuery
见:http://api.jquery.com/jQuery.post/
$.post("test.php", { name: "John"} );
GET Request by Javascript location
见:http://www.javascripter.net/faq/buttonli.htm
<input type=button
value="insert button text here"
onClick="self.location='Your_URL_here.php?name=hello'">
读取数据(PHP)
请参阅PHP教程阅读文章并获取:http://www.tizag.com/phpT/postget.php
的有用链接
http://php.net/manual/en/function.call-user-func.phphttp://php.net/manual/en/function.function-exists.php
如果你想让一个服务器请求你应该使用AJAX,所以你可以发送你想要的参数到服务器,它可以运行任何php你想要的这些参数。
示例:
<input type="text" id="name" value="..."/>
<input type="text" id="location" value="..."/>
<input type="button" onclick="ajaxFunction();" value="Submit" />
<div id="ajaxDiv"></div>
<script type="text/javascript">
function ajaxFunction(){
var ajaxRequest; // The variable that makes Ajax possible!
try{
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
} catch (e){
// Internet Explorer Browsers
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
// Create a function that will receive data sent from the server
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
var ajaxDisplay = document.getElementById('ajaxDiv');
ajaxDisplay.innerHTML = ajaxRequest.responseText;
}
}
var name = document.getElementById('name').value;
var location = document.getElementById('location').value;
var queryString = "?name=" + name + "&location=" + location;
ajaxRequest.open("POST", "some.php" + queryString, true);
ajaxRequest.send(null);
}
</script>
jQuery Ajax示例:http://api.jquery.com/jQuery.ajax/
$.ajax({
type: "POST",
url: "some.php",
data: { name: "John", location: "Boston" }
}).done(function( msg ) {
alert( "Data Saved: " + msg );
});
可以有一个函数文件,例如functions.php
显然也
<?php
myFunction($Name, $Location) {
// etc...
}
myFunction2() {
}
// ... many functions
?>
some.php
<?php include("functions.php");
$Name = $_POST['name'];
$Location = $_POST['location'];
myFunction($Name, $Location);
// make what you want with these variables...?>
使用ajax,一个简单的例子,