php 如何从提交按钮调用php函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17872848/
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 php function from submit button?
提问by viki
my filename is contacts.php that have two submit buttons;i want that if insert button is pressed insert function is called and if select is pressed select is called.i have written following code:
我的文件名是contacts.php,有两个提交按钮;我希望如果按下插入按钮,则调用插入函数,如果按下选择,则调用选择。我编写了以下代码:
//contacts.php
<?php
if(isset($_REQUEST['select']))
{
select();
}
else
{
insert();
}
?>
<html>
<body>
<form action="contacts.php">
<input type="text" name="txt"/>
<input type="submit" name="insert" value="insert" />
<input type="submit" name="select" value="select"/>
</form>
<?php
function select()
{
//do something
}
function insert()
{
//do something
}
?>
but it is not working .please help
但它不起作用。请帮忙
回答by Barmar
<?php
if (isset($_REQUEST['insert'])) {
insert();
} elseif (isset($_REQUEST['select'])) {
select();
}
Your code is calling insert()
even if no button is clicked, which will happen when the page is first displayed.
insert()
即使没有单击任何按钮,您的代码也会调用,这将在首次显示页面时发生。
回答by Matthew Herbst
As has been described by several people (summarizing the previous comments), you have two options.
正如几个人所描述的(总结之前的评论),您有两种选择。
The first is to send the data via POST or GET to the server directly and reserve (refresh) the page based on whatever you do inside select() and insert().
第一种是通过 POST 或 GET 直接将数据发送到服务器,并根据您在 select() 和 insert() 中执行的操作保留(刷新)页面。
While this is not the right place for a POST v GET discussion, convention is to use POST when sending data to the server. POST is slightly more secure because the information is not stored in the browser. Read more about the two here: http://www.w3schools.com/tags/ref_httpmethods.asp
虽然这不是 POST v GET 讨论的合适场所,但约定是在向服务器发送数据时使用 POST。POST 稍微更安全,因为信息不存储在浏览器中。在此处阅读有关两者的更多信息:http: //www.w3schools.com/tags/ref_httpmethods.asp
The second option is to use AJAX to accomplish your task without refreshing the web page. In short, AJAX uses Javascript methods that you place on your page to communicate with your server, thus avoiding the need for the PHP on the server to actually change anything on the page (which would require a refresh). A code example of AJAX can be found here: http://www.w3schools.com/ajax/tryit.asp?filename=tryajax_first
第二种选择是使用 AJAX 来完成您的任务,而无需刷新网页。简而言之,AJAX 使用放置在页面上的 Javascript 方法与服务器通信,从而避免服务器上的 PHP 实际更改页面上的任何内容(这需要刷新)。可以在这里找到 AJAX 的代码示例:http: //www.w3schools.com/ajax/tryit.asp?filename=tryajax_first
回答by Yogesh Suthar
If you are using return
inside function to return the result , you have to use echo to print the result while calling function.
如果您使用return
内部函数返回结果,则必须在调用函数时使用 echo 打印结果。
if(isset($_REQUEST['select']))
{
echo select();
}
elseif(isset($_REQUEST['insert']))
{
echo insert();
}
回答by liyakat
use post method because it is secure
使用 post 方法,因为它是安全的
//contacts.php
<?php
if(isset($_POST['select']))
{
select();
}
else
{
insert();
}
?>
<html>
<body>
<form action="contacts.php" method="post">
<input type="text" name="txt"/>
<input type="submit" name="insert" value="insert" />
<input type="submit" name="select" value="select"/>
</form>
<?php
function select()
{
//do something
}
function insert()
{
//do something
}
?>
回答by liyakat
<?php
$insert = $_POST['insert'];
$select = $_POST['select'];
if ($insert) {
insert();
}
if ($select) {
select();
}
else {
echo 'press any button...';
}
?>
<html>
<body>
<form action="contacts.php" method="post">
<input type="text" name="txt"/>
<input type="submit" name="insert" value="insert" />
<input type="submit" name="select" value="select"/>
</form>
<?php
function select() {
echo 'you pressed the [select] button';
exit;
}
function insert() {
echo 'you pressed the [insert] button';
exit;
}
?>