如何通过单击按钮调用 PHP 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20738329/
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
How to call a PHP function on the click of a button
提问by Razor
I have created a page called functioncalling.php that contains two buttons, Submitand Insert. As a beginner in PHP, I want to test which function is executed when a button gets clicked. I want the output to come on the same page. So I created two functions, one for each button. The source code for functioncalling.php is as follows:
我创建了一个名为 functioncalling.php 的页面,其中包含两个按钮Submit和Insert。作为 PHP 初学者,我想测试单击按钮时执行的函数。我希望输出出现在同一页面上。所以我创建了两个函数,每个按钮一个。functioncalling.php的源码如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<body>
<form action="functioncalling.php">
<input type="text" name="txt" />
<input type="submit" name="insert" value="insert" onclick="insert()" />
<input type="submit" name="select" value="select" onclick="select()" />
</form>
<?php
function select(){
echo "The select function is called.";
}
function insert(){
echo "The insert function is called.";
}
?>
The problem here is that I don't get any output after any of the buttons are clicked.
这里的问题是在单击任何按钮后我没有得到任何输出。
Where exactly am I going wrong?
我到底哪里错了?
采纳答案by Roopendra
Yes, you need Ajax here. Please refer to the code below for more details.
是的,这里需要 Ajax。请参阅下面的代码了解更多详情。
Change your markup like this
像这样更改您的标记
<input type="submit" class="button" name="insert" value="insert" />
<input type="submit" class="button" name="select" value="select" />
jQuery:
jQuery:
$(document).ready(function(){
$('.button').click(function(){
var clickBtnValue = $(this).val();
var ajaxurl = 'ajax.php',
data = {'action': clickBtnValue};
$.post(ajaxurl, data, function (response) {
// Response div goes here.
alert("action performed successfully");
});
});
});
In ajax.php
在 ajax.php 中
<?php
if (isset($_POST['action'])) {
switch ($_POST['action']) {
case 'insert':
insert();
break;
case 'select':
select();
break;
}
}
function select() {
echo "The select function is called.";
exit;
}
function insert() {
echo "The insert function is called.";
exit;
}
?>
回答by Moeed Farooqui
Button clicks are client sidewhereas PHP is executed server side, but you can achieve this by using Ajax:
按钮点击是客户端,而 PHP 是在服务器端执行的,但您可以使用Ajax实现这一点:
$('.button').click(function() {
$.ajax({
type: "POST",
url: "some.php",
data: { name: "John" }
}).done(function( msg ) {
alert( "Data Saved: " + msg );
});
});
In your PHP file:
在您的 PHP 文件中:
<?php
function abc($name){
// Your code here
}
?>
回答by DavidS
You should make the button call the same page and in a PHP section check if the button was pressed:
您应该让按钮调用同一页面,并在 PHP 部分检查按钮是否被按下:
HTML:
HTML:
<form action="theSamePage.php" method="post">
<input type="submit" name="someAction" value="GO" />
</form>
PHP:
PHP:
<?php
if($_SERVER['REQUEST_METHOD'] == "POST" and isset($_POST['someAction']))
{
func();
}
function func()
{
// do stuff
}
?>
回答by Jason W
You cannot call PHP functions like clicking on a button from HTML. Because HTML is on the client side while PHP runs server side.
您不能像单击 HTML 中的按钮那样调用 PHP 函数。因为 HTML 在客户端,而 PHP 在服务器端。
Either you need to use some Ajax or do it like as in the code snippet below.
您要么需要使用一些 Ajax,要么像下面的代码片段那样使用。
<?php
if ($_GET) {
if (isset($_GET['insert'])) {
insert();
} elseif (isset($_GET['select'])) {
select();
}
}
function select()
{
echo "The select function is called.";
}
function insert()
{
echo "The insert function is called.";
}
?>
You have to post your form data and then check for appropriate button that is clicked.
您必须发布表单数据,然后检查单击的适当按钮。
回答by user5253060
To show $message in your input:
要在您的输入中显示 $message:
<?php
if(isset($_POST['insert'])){
$message= "The insert function is called.";
}
if(isset($_POST['select'])){
$message="The select function is called.";
}
?>
<form method="post">
<input type="text" name="txt" value="<?php if(isset($message)){ echo $message;}?>" >
<input type="submit" name="insert" value="insert">
<input type="submit" name="select" value="select" >
</form>
To use functioncalling.php as an external file you have to include it somehow in your HTML document.
要将 functioncalling.php 用作外部文件,您必须以某种方式将它包含在您的 HTML 文档中。
回答by user2399308
Try this:
尝试这个:
if($_POST['select'] and $_SERVER['REQUEST_METHOD'] == "POST"){
select();
}
if($_POST['insert'] and $_SERVER['REQUEST_METHOD'] == "POST"){
insert();
}
回答by Vipul Tank
You can write like this in JavaScript or jQuery Ajax and call the file
您可以在 JavaScript 或 jQuery Ajax 中这样编写并调用文件
$('#btn').click(function(){
$.ajax({
url:'test.php?call=true',
type:'GET',
success:function(data){
body.append(data);
}
});
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.1/jquery.min.js"></script>
<form method='get' >
<input type="button" id="btn" value="click">
</form>
<?php
if(isset($_GET['call'])){
function anyfunction(){
echo "added";
// Your funtion code
}
}
?>
回答by linesarefuzzy
The onclickattribute in HTML calls JavaScript functions, not PHP functions.
onclickHTML 中的属性调用 JavaScript 函数,而不是 PHP 函数。
回答by Sanya Zahid
I was stuck in this and I solved it with a hidden field:
我被困在这个问题上,我用一个隐藏的字段解决了它:
<form method="post" action="test.php">
<input type="hidden" name="ID" value"">
</form>
In valueyou can add whatever you want to add.
在value你可以添加任何你想添加。
In test.php you can retrieve the value through $_Post[ID].
在 test.php 中,您可以通过$_Post[ID].
回答by bradleybrentmeyer
Use a recursive call where the form action calls itself. Then add PHP code in the same form to catch it. In foo.phpyour form will call foo.phpon post
在表单操作调用自身的地方使用递归调用。然后以相同的形式添加PHP代码来捕获它。在foo.php您的形式将调用foo.php上post
<html>
<body>
<form action="foo.php" method="post">
Once the form has been submitted it will call itself (foo.php) and you can catch it via the PHP predefined variable $_SERVERas shown in the code below
提交表单后,它将调用自身 ( foo.php),您可以通过 PHP 预定义变量捕获它$_SERVER,如下面的代码所示
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
echo "caught post";
}
?>
</form>
</body>
</html>

