在表单提交时调用特定的 PHP 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12328354/
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
Calling a particular PHP function on form submit
提问by Piklu Guha
I was trying to call a particular php function in submit of a form both the form and php scripts are in same page. My code is below.(it is not working and so I need help)
我试图在提交表单时调用特定的 php 函数,表单和 php 脚本都在同一页面中。我的代码如下。(它不起作用,所以我需要帮助)
<html>
<body>
<form method="post" action="display()">
<input type="text" name="studentname">
<input type="submit" value="click">
</form>
<?php
function display()
{
echo "hello".$_POST["studentname"];
}
?>
</body>
</html>
回答by The Alpha
In the following line
在以下行中
<form method="post" action="display()">
the action should be the name of your script and you should call the function, Something like this
动作应该是你的脚本的名称,你应该调用函数,像这样
<form method="post" action="yourFileName.php">
<input type="text" name="studentname">
<input type="submit" value="click" name="submit"> <!-- assign a name for the button -->
</form>
<?php
function display()
{
echo "hello ".$_POST["studentname"];
}
if(isset($_POST['submit']))
{
display();
}
?>
回答by kushalbhaktajoshi
you don't need this code
你不需要这个代码
<?php
function display()
{
echo "hello".$_POST["studentname"];
}
?>
Instead, you can check whether the form is submitted by checking the post variables using isset.
相反,您可以通过使用isset.
here goes the code
这是代码
if(isset($_POST)){
echo "hello ".$_POST['studentname'];
}
click here for the php manual for isset
单击此处获取isset的 php 手册
回答by Hernan Velasquez
Assuming that your script is named x.php, try this
假设你的脚本名为 x.php,试试这个
<?php
function display($s) {
echo $s;
}
?>
<html>
<body>
<form method="post" action="x.php">
<input type="text" name="studentname">
<input type="submit" value="click">
</form>
<?php
if($_SERVER['REQUEST_METHOD']=='POST')
{
display();
}
?>
</body>
</html>
回答by Kris
PHP is run on a server, Your browser is a client. Once the server sends all the info to the client, nothing can be done on the server until another request is made.
PHP 在服务器上运行,您的浏览器是客户端。一旦服务器将所有信息发送给客户端,在发出另一个请求之前,服务器上什么也做不了。
To make another request without refreshing the page you are going to have to look into ajax. Look into jQuery as it makes ajax requests easy
要在不刷新页面的情况下发出另一个请求,您将不得不查看 ajax。查看 jQuery,因为它使 ajax 请求变得容易
回答by Tarun
If you want to call a function on clicking of submit button then you have
to use ajax or jquery,if you want to call your php function after submission of form
you can do that as :
如果你想在点击提交按钮时调用一个函数,那么你必须
使用 ajax 或 jquery,如果你想在提交表单后调用你的 php 函数,你可以这样做:
<html>
<body>
<form method="post" action="display()">
<input type="text" name="studentname">
<input type="submit" value="click">
</form>
<?php
function display()
{
echo "hello".$_POST["studentname"];
}
if($_SERVER['REQUEST_METHOD']=='POST')
{
display();
}
?>
</body>
</html>
回答by GYaN
Write this code
写下这段代码
<?php
if(isset($_POST['submit'])){
echo 'Hello World';
}
?>
<html>
<body>
<form method="post">
<input type="text" name="studentname">
<input type="submit" name="submit" value="click">
</form>
</body>
</html>

