我想知道是否可以使用一个声明所有必要函数的PHP文件,而不是为每个函数创建一个唯一的文件。
例如,我有一个表单,它将文本提交到phpscript.php。另一种形式是发送日期。我会这样做:
<form id="text" method="post" action="textscript.php">
<textarea name="text"></textarea>
</form>
<form id="editForm" method="POST" action="datescript.php">
<input name="date" type="date" /><br />
<input id="submitDate" type="submit" /><br />
</form>
现在我想声明包含这两个过程的 functions.php,然后将此文件用于表单操作。我只是不知道如何实现这一点,因为我已经尝试过提供一个 URL 参数。
您可以查看前端控制器模式来调度"函数调用"。
是的,你可以这样做!
只需创建一个类似 functions.php 的文件,并在您要使用这些函数的每个文件中使用 include
.有关如何使用包含的详细信息,请查看包含 PHP.net 文档。
例
<?php
include ("functions.php");
?>
您可以通过添加隐藏的输入值来执行此操作。
<form id="editForm" method="POST" action="functions.php">
<input name="date" type="date" /><br />
<input id="submitDate" type="submit" /><br />
<input type-'hidden' name='function' value='datescript' />
</form>
<form id="text" method="post" action="functions.php">
<textarea name="text"></textarea>
<input type-'hidden' name='function' value='textscript' />
</form>
然后在您的函数中.php您可以从$_POST['function']
和
if($_POST['function'] == 'datescript'){
//do the relevant things
}
if($_POST['function'] == 'textscript'){
//do the relevant things
}
希望这有帮助:-)