25 php
我试图在表单操作中从PHP脚本运行一个函数.
<?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>';
?>
Run Code Online (Sandbox Code Playgroud)
我回应表单,但我想要从username.php调用函数"username"来执行.我怎么能以类似的方式做到这一点?
Jer*_*y L 21
<?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>';
?>
Run Code Online (Sandbox Code Playgroud)
您需要在表单发送到的页面中运行该功能.
小智 13
在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
Run Code Online (Sandbox Code Playgroud)
HTML中标记的action参数不应引用您要运行的PHP函数.操作应该引用Web服务器上的一个页面,该页面将处理表单输入并将新HTML返回给用户.这可以与输出表单的PHP脚本位于同一位置,或者某些人更喜欢创建单独的PHP文件来处理操作.
基本过程是相同的两种方式:
一个简单的例子是:
<?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>
';
}
?>
Run Code Online (Sandbox Code Playgroud)
对于未来的信息,我建议你阅读的PHP教程:http://php.net/tut.php
甚至有关于处理表格的部分.
我不确定我理解你想要实现什么,因为我们没有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>
Run Code Online (Sandbox Code Playgroud)
这会将结果发布在同一页面中.因此,首次显示页面时,只显示空表单,如果按下提交,则文本字段将位于$textfield变量中,您可以根据需要再次显示该字段.
我不知道该username()函数是否应该返回您应该发送结果的URL,但这是您在action表单属性中所需要的.我已将结果放在示例段落中,以便您了解如何显示结果.请参阅"您的用户名是..."部分.
//编辑:
如果要在不离开页面的情况下发送值,则需要使用AJAX.在StackOverflow或Google上搜索jQuery.
您可能希望让函数返回用户名而不是回显它.但是如果你绝对想要从函数中回应它,只需<?php username() ?>在HTML表单中调用它.
我认为在进一步研究之前,您需要了解页面客户端 - 服务器进程的流程.假设上面的示例代码名为form.php.
我觉得应该是这样。。
<?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>';
?>
Run Code Online (Sandbox Code Playgroud)