点击html按钮运行PHP函数

Run PHP function on html button click

本文关键字:PHP 函数 运行 按钮 html 点击      更新时间:2023-09-26

我需要在单击按钮时运行一些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,一个简单的例子,

<button id="button">Get Data</button>
Javascript

var button = document.getElementById("button");
button.addEventListener("click" ajaxFunction, false);
var ajaxFunction = function () {
    // ajax code here
}

或者查看jquery ajax http://api.jquery.com/jQuery.ajax/

<?php
if (isset($_POST['str'])){
function printme($str){
echo $str;
}
printme("{$_POST['str']}");
}
?>
<form action="<?php $_PHP_SELF ?>" method="POST">
<input type="text" name="str" /> <input type="submit" value="Submit"/>
</form>

在PHP文件上使用AJAX请求,然后在页面上显示结果,而无需重新加载。

http://api.jquery.com/load/如果您不需要任何POST数据,这是一个简单的解决方案。

这取决于你想运行什么函数。如果你需要在服务器端完成一些事情,比如查询数据库或在会话中设置一些东西,或者任何不能在客户端完成的事情,你需要AJAX,否则你可以在客户端使用JavaScript完成。当你可以在客户端做你需要做的事情时,不要让服务器工作。

jQuery提供了一种简单的ajax方法:http://api.jquery.com/jQuery.ajax/