在外部php文件中将表单动作设置为函数 [英] setting form action as function in external php file

查看:99
本文介绍了在外部php文件中将表单动作设置为函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是PHP新手(有点),我查了一下,找不到任何完全符合我的问题的信息,所以在这里;



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

 < form name = tryLoginaction =logIn()method =post> 
用户名:< input type =textname =usernameplaceholder =username ..>< br>
密码:< input type =textname =passwordplaceholder =password ..>< br>
< input type =submitvalue =提交>
< / form>

在这里你可以看到我试图将这个动作设置为一个函数logIn(),我已经包含在这个文件的头文件中。



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

 函数logIn()
{
if($ _ POST ['username'] ==shane&& $ _POST ['password '] ==shane)
{
$ _SESSION ['loggedIn'] ='1';
$ _SESSION ['user'] = $ _POST ['username'];
}

header(Location:/index.php);
}

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

$ / code>

(忽略任何你不应该这样做,那么做,我只是在这里画一个图片)。

所以基本上我希望表单提交给那个特定的函数,这可能吗?我在这里做了一些根本性的错误吗?

但是您可以动态地决定在PHP端执行哪些操作,具体取决于使用PHP代码提交的表单。一种方法是用一个隐藏的输入定义您的逻辑,以便您可以在同一页面上处理不同的操作,如下所示:

 << ; form name =tryLoginaction =index.phpmethod =post> 
< input type =hiddenname =actionvalue =login/>
用户名:< input type =textname =usernameplaceholder =username ..>< br />
密码:< input type =textname =passwordplaceholder =password ..>< br />
< input type =submitvalue =提交>
< / form>

< form name =otherformaction =index.phpmethod =post>
< input type =hiddenname =actionvalue =otheraction/>
输入内容:< input type =textname =something>< br />
< input type =submitvalue =提交>
< / form>

然后在您的PHP中:



<$ p $ b $'$'$'$'$'$'$'$'$'$'$'$'$'$'$'$'$'$'$'$'$' b login();
休息;
case'otheraction':
dosomethingelse();
休息;
}
}


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;

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.

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

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

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

这篇关于在外部php文件中将表单动作设置为函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆