0

我想知道是否可以使用单个 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 参数。

4

3 回答 3

2

您可以查看前端控制器模式来调度“函数调用”。

于 2012-06-29T10:58:50.903 回答
0

是的,你可以这样做!

只需创建一个类似functions.php 的文件,然后在每个要使用这些函数的文件中使用include. 查看PHP.net文档的 include 以获取有关如何使用 include 的更多信息。

例子

<?php

include ("functions.php");

?>
于 2012-06-29T10:55:38.343 回答
0

您可以通过添加隐藏的输入值来做到这一点。

<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>

然后在你的functions.php中,你可以从$_POST['function']

    if($_POST['function'] == 'datescript'){
      //do the relevant things
    }
    if($_POST['function'] == 'textscript'){
      //do the relevant things
    }

希望这可以帮助 :-)

于 2012-06-29T11:00:41.307 回答