将表单动作设置为外部php文件中的函数

时间:2022-10-16 10:48:50

I'm new to PHP (somewhat), and i've had a look around and can't find any information which caters exactly to my questions, so here it is;

我对PHP(有点)还不熟悉,我四处看看,找不到任何能完全满足我的问题的信息,就在这里;

Let's say i declare a form, with 2 fields and a submit button;

假设我声明了一个表单,有两个字段和一个提交按钮;

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

Here you can see i've tried to set the action as a function "logIn()", which i've included already in the header of this file.

这里可以看到,我尝试将操作设置为“logIn()”函数,我已经在这个文件的头中包含了它。

In an external php file i've got the following;

在一个外部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");
}

(Ignore any "y'shouldn't do this, do that", i'm just painting a picture here).

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

So basically i want the form to submit to that particular function, is that possible? Am i doing something fundamentally wrong here?

基本上我想让表单提交给那个特定的函数,这可能吗?我在这里做了什么根本性的错误吗?

3 个解决方案

#1


3  

As others have said, you can't direct a post to a function automatically, but you can dynamically decide what to do on the PHP side depending on which form is submitted with PHP code. One way is to define your logic with a hidden input so you can handle different actions on the same page, like this:

正如其他人所说,您不能将post自动指向函数,但是您可以根据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>

and then in your PHP:

在PHP中

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

#2


2  

No you submit the form to a page and you run your function if the form is submitted:

不,您将表单提交到一个页面,如果提交了表单,则运行您的函数:

Html:

Html:

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

PHP (index.php):

PHP(index . PHP):

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

#3


2  

To directly answer your question, yes, you are doing something wrong. However, it's easily fixable.

直接回答你的问题,是的,你做错了什么。但是,它很容易可以解决的。

The action on the form is where it submits the form - i.e. the page to send the request. As you're saying that your code is "at the top of the page", you'll want to submit the form back to the page it's on. So, you could either put the full URL of the page in the action or just leave it blank:

表单上的操作是提交表单的地方——也就是发送请求的页面。正如您所说的,您的代码是“在页面的顶部”,您将希望将表单提交回它所在的页面。因此,你可以将页面的完整URL放在操作中,或者将其留空:

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

To handle the submission, PHP doesn't have a way to directly call a function from client-side code, however, you can process the request in a more request-handling way by sending a hidden field with the current "task".

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

For instance, in the HTML form, try adding:

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

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

Then, in the PHP code, try adding this:

然后,在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();
    }
}

This code will check if the form has been submitted by checking if the task field has been posted. Then, it will check the value. If it's logIn, the logIn() function will be called. Alternatively, if it's logOut, the logOut() function will be called.

此代码将通过检查任务字段是否已发布来检查表单是否已提交。然后,它将检查值。如果是logIn,则将调用logIn()函数。另外,如果是注销,则会调用logOut()函数。

To create the log-out form, you would adjust the action accordingly and add a hidden field to that one like the above, but with the value of logOut.

要创建注销表单,您需要相应地调整操作,并在上面这样的表单中添加一个隐藏字段,但是要使用logOut值。

#1


3  

As others have said, you can't direct a post to a function automatically, but you can dynamically decide what to do on the PHP side depending on which form is submitted with PHP code. One way is to define your logic with a hidden input so you can handle different actions on the same page, like this:

正如其他人所说,您不能将post自动指向函数,但是您可以根据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>

and then in your PHP:

在PHP中

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

#2


2  

No you submit the form to a page and you run your function if the form is submitted:

不,您将表单提交到一个页面,如果提交了表单,则运行您的函数:

Html:

Html:

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

PHP (index.php):

PHP(index . PHP):

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

#3


2  

To directly answer your question, yes, you are doing something wrong. However, it's easily fixable.

直接回答你的问题,是的,你做错了什么。但是,它很容易可以解决的。

The action on the form is where it submits the form - i.e. the page to send the request. As you're saying that your code is "at the top of the page", you'll want to submit the form back to the page it's on. So, you could either put the full URL of the page in the action or just leave it blank:

表单上的操作是提交表单的地方——也就是发送请求的页面。正如您所说的,您的代码是“在页面的顶部”,您将希望将表单提交回它所在的页面。因此,你可以将页面的完整URL放在操作中,或者将其留空:

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

To handle the submission, PHP doesn't have a way to directly call a function from client-side code, however, you can process the request in a more request-handling way by sending a hidden field with the current "task".

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

For instance, in the HTML form, try adding:

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

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

Then, in the PHP code, try adding this:

然后,在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();
    }
}

This code will check if the form has been submitted by checking if the task field has been posted. Then, it will check the value. If it's logIn, the logIn() function will be called. Alternatively, if it's logOut, the logOut() function will be called.

此代码将通过检查任务字段是否已发布来检查表单是否已提交。然后,它将检查值。如果是logIn,则将调用logIn()函数。另外,如果是注销,则会调用logOut()函数。

To create the log-out form, you would adjust the action accordingly and add a hidden field to that one like the above, but with the value of logOut.

要创建注销表单,您需要相应地调整操作,并在上面这样的表单中添加一个隐藏字段,但是要使用logOut值。