【问题标题】:setting form action as function in external php file将表单操作设置为外部 php 文件中的函数
【发布时间】:2012-12-31 02:16:22
【问题描述】:

我是 PHP 新手(有点),我环顾四周,找不到任何完全符合我的问题的信息,所以在这里;

假设我声明了一个包含 2 个字段和一个提交按钮的表单;

<form name = "tryLogin" action = "logIn()" method = "post">
            Username: <input type = "text" name = "username" placeholder = "username.."><br>
            Password: <input type = "text" name = "password" placeholder = "password.."><br>
            <input type = "submit" value = "Submit">
</form>

在这里您可以看到我尝试将操作设置为函数“logIn()”,我已经将其包含在此文件的标题中。

在一个外部 php 文件中,我有以下内容;

function logIn()
{
if($_POST['username'] == "shane" && $_POST['password'] == "shane")
{
    $_SESSION['loggedIn'] = '1';
    $_SESSION['user'] = $_POST['username'];
}

header ("Location: /index.php");
}

function logOut()
{
$_SESSION['loggedIn'] = '0';
header ("Location: /index.php");
}

(忽略任何“你不应该做这个,做那个”,我只是在这里画画)。

所以基本上我希望表单提交给该特定功能,这可能吗?我在这里做一些根本错误的事情吗?

【问题讨论】:

  • 是的,你不能那样做。它不会在响应正文中发送,一旦服务器向您发送响应,您就会切断与服务器的所有联系
  • 您需要编写一个登录页面并在表单操作中使用它。获取此页面上的登录详细信息,然后您可以从该登录页面调用登录功能。

标签: php html forms


【解决方案1】:

正如其他人所说,您不能自动将帖子定向到函数,但您可以根据使用 PHP 代码提交的表单动态决定在 PHP 端做什么。一种方法是使用隐藏输入定义逻辑,以便您可以在同一页面上处理不同的操作,如下所示:

<form name="tryLogin" action="index.php" method="post">
            <input type="hidden" name="action" value="login" />
            Username: <input type="text" name="username" placeholder="username.."><br />
            Password: <input type="text" name="password" placeholder="password.."><br />
            <input type="submit" value="Submit">
</form>

<form name="otherform" action="index.php" method="post">
            <input type="hidden" name="action" value="otheraction" />
            Type something: <input type="text" name="something"><br />
            <input type="submit" value="Submit">
</form>

然后在你的 PHP 中:

if (isset($_POST['action'])) {
    switch($_POST['action']) {
    case 'login':
        login();
        break;
    case 'otheraction':
        dosomethingelse();
        break;
    }
}

【讨论】:

    【解决方案2】:

    不,您将表单提交到页面并在表单提交后运行您的函数:

    HTML:

    <form action="index.php" method="post">
    

    PHP (index.php):

    if ($_SERVER['REQUEST_METHOD'] == "POST"){
        // Run your function
        login();
    }
    

    【讨论】:

      【解决方案3】:

      直接回答你的问题,是的,你做错了。但是,它很容易修复。

      表单上的 action 是它提交表单的地方 - 即发送请求的页面。正如您所说,您的代码位于“页面顶部”,您需要将表单提交回它所在的页面。因此,您可以将页面的完整 URL 放入操作中,也可以将其留空:

      <form name = "tryLogin" action = "" method = "post">
      

      为了处理提交,PHP 没有办法直接从客户端代码调用函数,但是,您可以通过发送带有当前“任务”的隐藏字段以更具请求处理的方式处理请求”。

      例如,在 HTML 表单中,尝试添加:

      <input type="hidden" name="task" value="logIn" />
      

      然后,在 PHP 代码中,尝试添加:

      if (isset($_POST['task'])) {
          if ($_POST['task'] == 'logIn') {
              // the user is trying to log in; call the logIn() function
              logIn();
          } else if ($_POST['task'] == 'logOut') {
              // the user is trying to log out; call the logOut() function
              logOut();
          }
      }
      

      此代码将通过检查task 字段是否已发布来检查表单是否已提交。然后,它将检查该值。如果是logIn,则将调用logIn() 函数。或者,如果是logOut,则将调用logOut() 函数。

      要创建注销表单,您需要相应地调整操作并像上面一样向该表单添加一个隐藏字段,但值为logOut。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-07-30
        • 1970-01-01
        相关资源
        最近更新 更多