单击按钮时运行 PHP 函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5798877/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Run a PHP function upon button click?
提问by Voldemort
I want to run a PHP function with an HTML button click.
我想通过单击 HTML 按钮运行 PHP 函数。
I can call a PHP script this way:
我可以这样调用 PHP 脚本:
<form action="action.php" method="post">
Name: <input type="text" name="txt"/>
<input type="submit" />
</form>
But I don't want to invoke "action.php". I just want to call the PHP function I defined in this page. Can this be done?
但我不想调用“action.php”。我只想调用我在此页面中定义的 PHP 函数。这能做到吗?
回答by domnissient
The method I prefer to use is a mixture of jQuery/ajax calling an Object Oriented PHP class.
我更喜欢使用的方法是 jQuery/ajax 混合调用面向对象的 PHP 类。
I have a jQuery listener/trigger for the button press with something like this:
我有一个用于按下按钮的 jQuery 侦听器/触发器,如下所示:
$('#buttonId').live('click', function() {
$.get('api.php?functionName=test&inputvar=something');
return false;
});
That will call the api.php file through ajax and prevent any further action, such as form submission.
这将通过 ajax 调用 api.php 文件并阻止任何进一步的操作,例如表单提交。
Then in the PHP file you could always do something basic like:
然后在 PHP 文件中你总是可以做一些基本的事情,比如:
if ($_REQUEST['functionName'] == 'test') {
test();
}
or if you have huge classes and alot of dynamic input you could do something more interesting like:
或者如果你有大量的类和大量的动态输入,你可以做一些更有趣的事情,比如:
$functionName = $_REQUEST['functionName'];
if (method_exists($myClassInstance, $functionName))
$myClassInstance->$functionName();
There are numerous ways to approach this, but these are my favorites. Another alternative is the Extjs framework which is built for this kind of activity but if you are not familiar with it already and the project is not 'huge' I would not concern myself with it.
有很多方法可以解决这个问题,但这些是我的最爱。另一种选择是 Extjs 框架,它是为这种活动而构建的,但如果你不熟悉它并且项目不是“巨大的”,我不会关心它。
Lastly, if you are needing to get a response back from the php file such as json results, then instead of using the jQuery: $.get()
function, use the function $.getJSON
最后,如果您需要从 php 文件中获取响应,例如 json 结果,那么不要使用 jQuery:$.get()
函数,而是使用该函数$.getJSON
Hope that helps :)
希望有帮助:)
回答by Max
You need to do this on your end. Process the post and call the function in the action.php page on your own. You can also create a separate php page to call a function defined in action.php.
你需要这样做。处理帖子并自行调用action.php页面中的函数。您还可以创建一个单独的 php 页面来调用 action.php 中定义的函数。
回答by Tim Joyce
You could easily do it with jquery
你可以很容易地用 jquery 做到这一点
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
<script type="text/javascript">
function submitform(){
$('#myForm').post("pageName.php", $("#myForm").serialize());
}
</script> </head>
<form action="pageName.php" method="post" id"myForm">
Name: <input type="text" name="txt"/>
<input type="submit" />
</form>
回答by DJafari
You should include a.php In action.php, For example :
你应该在 action.php 中包含一个.php,例如:
action.php :
动作.php :
include( 'a.php' );
//Your codes
if you want for example ok() function in a.php, now a.php exist in action.php
如果你想在 a.php 中例如 ok() 函数,现在 a.php 存在于 action.php
回答by Ignacio Vazquez-Abrams
This won't work. PHP is pretty much done processing by the time the user handles the page. You'd have to use AJAX to send the data to the server and retrieve the response.
这行不通。到用户处理页面时,PHP 几乎已完成处理。您必须使用 AJAX 将数据发送到服务器并检索响应。
回答by Axe
You can have the following approaches:
您可以采用以下方法:
1> Use AJAX to load "action.php" with the data entered in the fields as POST to the page and then take the response and put it into the body; or,
1>使用AJAX加载“action.php”,将字段中输入的数据作为POST发送到页面,然后将响应放入正文中;或者,
2> You cannot make it work as PHP is a server-side programming language and HTML which you are trying to use with PHP is a client side programming language. So there is no way to make it work other than AJAX.
2> 你不能让它工作,因为 PHP 是一种服务器端编程语言,而你试图与 PHP 一起使用的 HTML 是一种客户端编程语言。所以除了 AJAX 之外,没有办法让它工作。
回答by Ben
This is a little complicated. You can either submit the form to itself, check to see if the post variable is set (if it is, then the page is being run because the form was submitted to it), or you can use ajax. The first is an easier solution, and the second doesn't require a page reload. Pick your poison ;)
这有点复杂。您可以将表单提交给自身,检查是否设置了 post 变量(如果是,则页面正在运行,因为表单已提交给它),或者您可以使用 ajax。第一个是更简单的解决方案,第二个不需要重新加载页面。选择你的毒药;)
Look at thisfor the first solution, and look at jquery's ajax functionsfor the second.
第一个解决方案看这个,第二个解决方案看jquery 的 ajax 函数。
回答by Jess McKenzie
You could try:
你可以试试:
<form action="pageName.php/functionName" method="post">
Name: <input type="text" name="txt"/>
<input type="submit" />
</form>