如何在表单操作中执行 PHP 函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/595358/
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 can I execute a PHP function in a form action?
提问by
I am trying to run a function from a PHP script in the form action.
我正在尝试从表单操作中的 PHP 脚本运行一个函数。
My code:
我的代码:
<?php
require_once ( 'username.php' );
echo '
<form name="form1" method="post" action="username()">
<p>
<label>
<input type="text" name="textfield" id="textfield">
</label>
</p>
<p>
<label>
<input type="submit" name="button" id="button" value="Submit">
</label>
</p>
</form>';
?>
I echo the form but I want the function "username" which is called from username.php to be executed. how can I do this in a simliar way to the above?
我回显了表单,但我希望执行从 username.php 调用的函数“用户名”。我怎样才能以与上述类似的方式做到这一点?
回答by Jeremy L
<?php
require_once ( 'username.php' );
if (isset($_POST['textfield'])) {
echo username();
return;
}
echo '
<form name="form1" method="post" action="">
<p>
<label>
<input type="text" name="textfield" id="textfield">
</label>
</p>
<p>
<label>
<input type="submit" name="button" id="button" value="Submit">
</label>
</p>
</form>';
?>
You need to run the function in the page the form is sent to.
您需要在表单发送到的页面中运行该函数。
回答by Kjartan
In PHP functions will not be evaluated inside strings, because there are different rules for variables.
在 PHP 中,函数不会在字符串内部求值,因为变量有不同的规则。
<?php
function name() {
return 'Mark';
}
echo 'My name is: name()'; // Output: My name is name()
echo 'My name is: '. name(); // Output: My name is Mark
The action parameter to the tag in HTML should not reference the PHP function you want to run. Action should refer to a page on the web server that will process the form input and return new HTML to the user. This can be the same location as the PHP script that outputs the form, or some people prefer to make a separate PHP file to handle actions.
HTML 中标记的 action 参数不应引用您要运行的 PHP 函数。Action 应该引用 Web 服务器上的一个页面,该页面将处理表单输入并将新的 HTML 返回给用户。这可以与输出表单的 PHP 脚本位于同一位置,或者有些人更喜欢制作单独的 PHP 文件来处理操作。
The basic process is the same either way:
无论哪种方式,基本过程都是相同的:
- Generate HTML form to the user.
- User fills in the form, clicks submit.
- The form data is sent to the locations defined by action on the server.
- The script validates the data and does something with it.
- Usually a new HTML page is returned.
- 为用户生成 HTML 表单。
- 用户填写表单,点击提交。
- 表单数据被发送到服务器上的 action 定义的位置。
- 该脚本验证数据并对其进行处理。
- 通常会返回一个新的 HTML 页面。
A simple example would be:
一个简单的例子是:
<?php
// $_POST is a magic PHP variable that will always contain
// any form data that was posted to this page.
// We check here to see if the textfield called 'name' had
// some data entered into it, if so we process it, if not we
// output the form.
if (isset($_POST['name'])) {
print_name($_POST['name']);
}
else {
print_form();
}
// In this function we print the name the user provided.
function print_name($name) {
// $name should be validated and checked here depending on use.
// In this case we just HTML escape it so nothing nasty should
// be able to get through:
echo 'Your name is: '. htmlentities($name);
}
// This function is called when no name was sent to us over HTTP.
function print_form() {
echo '
<form name="form1" method="post" action="">
<p><label><input type="text" name="name" id="textfield"></label></p>
<p><label><input type="submit" name="button" id="button" value="Submit"></label></p>
</form>
';
}
?>
For future information I recommend reading the PHP tutorials: http://php.net/tut.php
对于未来的信息,我建议阅读 PHP 教程:http: //php.net/tut.php
There is even a section about Dealing with forms.
甚至还有一个关于处理表单的部分。
回答by lpfavreau
I'm not sure I understand what you are trying to achieve as we don't have what username()is supposed to return but you might want to try something like that. I would also recommend you don't echo whole page and rather use something like that, it's much easier to read and maintain:
我不确定我是否理解你想要实现的目标,因为我们没有username()应该返回的东西,但你可能想尝试类似的东西。我还建议您不要回显整个页面,而是使用类似的东西,它更易于阅读和维护:
<?php
require_once ( 'username.php' );
if (isset($_POST)) {
$textfield = $_POST['textfield']; // this will get you what was in the
// textfield if the form was submitted
}
?>
<form name="form1" method="post" action="<?php echo($_SERVER['PHP_SELF']) ?">
<p>Your username is: <?php echo(username()) ?></p>
<p>
<label>
<input type="text" name="textfield" id="textfield">
</label>
</p>
<p>
<label>
<input type="submit" name="button" id="button" value="Submit">
</label>
</p>
</form>
This will post the results in the same page. So first time you display the page, only the empty form is shown, if you press on submit, the textfield field will be in the $textfieldvariable and you can display it again as you want.
这将在同一页面中发布结果。所以第一次显示页面时,只显示空表单,如果你按下提交,文本字段字段将在$textfield变量中,你可以根据需要再次显示它。
I don't know if the username()function was supposed to return you the URL of where you should send the results but that's what you'd want in the actionattribute of your form. I've put the result down in a sample paragraph so you see how you can display the result. See the "Your username is..." part.
我不知道该username()函数是否应该返回您应该将结果发送到何处的 URL,但这就是您在action表单属性中想要的。我已将结果放在示例段落中,以便您了解如何显示结果。请参阅“您的用户名是...”部分。
// Edit:
// 编辑:
If you want to send the value without leaving the page, you want to use AJAX. Do a search on jQuery on StackOverflow or on Google.
如果你想在不离开页面的情况下发送值,你想使用 AJAX。在 StackOverflow 或 Google 上搜索 jQuery。
You would probably want to have your function return the username instead of echo it though. But if you absolutely want to echo it from the function, just call it like that <?php username() ?>in your HTML form.
您可能希望您的函数返回用户名而不是回显它。但是,如果您绝对想从函数中回显它,只需<?php username() ?>在 HTML 表单中像这样调用它即可。
I think you will need to understand the flow of the client-server process of your pages before going further. Let's say that the sample code above is called form.php.
我认为在继续之前,您需要了解页面的客户端 - 服务器进程的流程。假设上面的示例代码称为 form.php。
- Your form.php is called.
- The form.php generates the HTML and is sent to the client's browser.
- The client only sees the resulting HTML form.
- When the user presses the button, you have two choices: a) let the browser do its usual thing so send the values in the form fields to the page specified in action (in this case, the URL is defined by PHP_SELF which is the current page.php). Or b) use javascript to push this data to a page without refreshing the page, this is commonly called AJAX.
- A page, in your case, it could be changed to username.php, will then verify against the database if the username exists and then you want to regenerate HTML that contains the values you need, back to step 1.
- 您的 form.php 被调用。
- form.php 生成 HTML 并发送到客户端的浏览器。
- 客户端只能看到生成的 HTML 表单。
- 当用户按下按钮时,您有两种选择:a) 让浏览器做它通常的事情,将表单字段中的值发送到操作中指定的页面(在这种情况下,URL 由 PHP_SELF 定义,它是当前的页面.php)。或者 b) 使用 javascript 将此数据推送到页面而不刷新页面,这通常称为 AJAX。
- 一个页面,在你的情况下,它可以更改为 username.php,然后将根据数据库验证用户名是否存在,然后你想重新生成包含你需要的值的 HTML,回到步骤 1。
回答by Nytmode
I think it should be like this..
我觉得应该是这样。。
<?php
require_once ( 'username.php' );
echo '
<form name="form1" method="post" action="<?php username() ?>">
<p>
<label>
<input type="text" name="textfield" id="textfield">
</label>
</p>
<p>
<label>
<input type="submit" name="button" id="button" value="Submit">
</label>
</p>
</form>';
?>
回答by Luke
It's better something like this...post the data to the self page and maybe do a check on user input.
最好是这样的......将数据发布到自我页面,也许会对用户输入进行检查。
<?php
require_once ( 'username.php' );
if(isset($_POST)) {
echo "form post"; // ex $_POST['textfield']
}
echo '
<form name="form1" method="post" action="' . $_SERVER['PHP_SELF'] . '">
<p>
<label>
<input type="text" name="textfield" id="textfield">
</label>
</p>
<p>
<label>
<input type="submit" name="button" id="button" value="Submit">
</label>
</p>
</form>';
?>
回答by macki
Is it really a function call on the action attribute? or it should be on the onSUbmit attribute, because by convention action attribute needs a page/URL.
它真的是对 action 属性的函数调用吗?或者它应该在 onSUbmit 属性上,因为按照惯例 action 属性需要一个页面/URL。
I think you should use AJAX with that,
我认为你应该使用 AJAX,
There are plenty PHP AJAX Frameworks to choose from
有很多 PHP AJAX 框架可供选择
http://www.phplivex.com/example/submitting-html-forms-with-ajax
http://www.phplivex.com/example/submitting-html-forms-with-ajax
http://www.xajax-project.org/en/docs-tutorials/learn-xajax-in-10-minutes/
http://www.xajax-project.org/en/docs-tutorials/learn-xajax-in-10-minutes/
ETC.
等等。
Or the classic way
或者经典的方式
http://www.w3schools.com/php/php_ajax_php.asp
http://www.w3schools.com/php/php_ajax_php.asp
I hope these links help
我希望这些链接有帮助
回答by Davide Gualano
You can put the username() function in another page, and send the form to that page...
您可以将 username() 函数放在另一个页面中,然后将表单发送到该页面...

