简体   繁体   English

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

[英]setting form action as function in external php file

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; 假设我声明了一个表单,包含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>

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? 我在这里做了一些根本错误的事吗?

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. 正如其他人所说,你不能自动将帖子定向到一个函数,但你可以动态地决定在PHP端做什么,具体取决于使用PHP代码提交的表单。 One way is to define your logic with a hidden input so you can handle different actions on the same page, like this: 一种方法是使用隐藏输入定义逻辑,以便您可以在同一页面上处理不同的操作,如下所示:

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

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();
}

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 - ie 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. 此代码将通过检查task字段是否已过帐来检查表单是否已提交。 Then, it will check the value. 然后,它将检查值。 If it's logIn , the logIn() function will be called. 如果是logIn ,将logIn()函数。 Alternatively, if it's logOut , the logOut() function will be called. 或者,如果它是logOut ,则将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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM