在HTML按钮单击运行PHP函数 [英] Run PHP function on html button click

查看:281
本文介绍了在HTML按钮单击运行PHP函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要的是单击按钮时运行一些PHP函数。我知道这是不应该是PHP的使用,而JS应该这样做,但我有什么功能都在当用户要求它从一台服务器收集数据做什么。具体地,它得到了一些用户数据,并将其写入到文件中,并且用户应该决定哪些数据将被收集的。结果,
我怎样才能做到这一点?我看到后运行PHP文件在按钮单击但我仍然不知道如何使用它。结果
我正在学习,所以请不要太苛刻

I need to run some PHP function when a button is clicked. I know this is not supposed to be php's use, rather js should do it, but what my functions are doing in gathering data from a server when the user asks it. Specifically, it gets some user data and writes it to a file, and the user should decide what data will be gathered.
How can I do this? I saw the post Run PHP File On Button Click but I am still not sure how to use it.
I'm learning, so please don't be too harsh

我曾尝试的onclick()和各种各样的事情,但它并没有导致任何有用

I have tried onclick() and all sorts of things but it didn't lead to anything useful

推荐答案

当你通过HTTP请求访问它无论是GET,POST的PHP文件运行时,PUT。

A php file is run whenever you access it via an HTTP request be it GET,POST, PUT.

您可以使用jQuery / Ajax来点击一个按钮发送请求,甚至只是改变浏览器的网址导航到PHP的地址。

You can use JQuery/Ajax to send a request on a button click, or even just change the URL of the browser to navigate to the php address.

根据在POST发送的数据/让你都可以运行不同的功能switch语句。

您可以在这里利用code:<一href=\"http://stackoverflow.com/questions/1005857/how-to-call-php-function-from-string-stored-in-a-variable\">How从存储在变量与switch语句一起字符串调用PHP函数自动调用根据发送的数据相应的功能。

You can utilize the code here: How to call PHP function from string stored in a Variable along with a switch statement to automatically call the appropriate function depending on data sent.

因此​​,对PHP端你可以有这样的事情:

So on PHP side you can have something like this:

<?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请求:

and you can make the simplest get request using the address bar as testing:

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请求

请参阅: http://api.jquery.com/jQuery.get/

$.get("test.cgi", { name: "John"})
.done(function(data) {
  alert("Data Loaded: " + data);
});

通过jQuery POST请求

请参阅: http://api.jquery.com/jQuery.post/

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

通过Javascript位置GET请求

请参阅:<一href=\"http://www.javascripter.net/faq/buttonli.htm\">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 Turotial阅读后,并得到: http://www.tizag.com/phpT/postget.php

<一个href=\"http://php.net/manual/en/function.call-user-func.php\">http://php.net/manual/en/function.call-user-func.php
<一href=\"http://php.net/manual/en/function.function-exists.php\">http://php.net/manual/en/function.function-exists.php

这篇关于在HTML按钮单击运行PHP函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆