将PHP表单中的HTML表单数据传递给其他3个PHP文件

时间:2022-10-16 12:58:42

I'm building a website with a simple login interface. The user can log in as one of 3 different users types, A B and C using 1 form. The code inside the form for handling this is:

我正在建立一个带有简单登录界面的网站。用户可以使用1个表单以3种不同的用户类型A B和C之一登录。表单中用于处理此问题的代码是:

                    <input type="radio" name="user_type" value="a" id="a">
                    <label for="a">A</label>

                    <input type="radio" name="user_type" value="b" id="b">
                    <label for="b">B</label>

                    <input type="radio" name="user_type" value="c" id="c">
                    <label for="c">C</label>

Depending on the user type, the login page should redirect the user to one of three PHP files on click of the form submit button. I know in PHP you can use

根据用户类型,登录页面应该在单击表单提交按钮时将用户重定向到三个PHP文件之一。我知道在PHP中你可以使用

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

In the login page for leading to one other file. And use

在登录页面中导致另一个文件。并使用

<?php
if(isset($_POST['submit']))
{
    $firstname = $_POST['firstname'];
    $lastname = $_POST['lastname'];
    echo $firstname . "<br/>";
    echo $lastname;
}
?> 

In otherFile.php to retrieve form data. But I want to do this for one of three PHP files, depending on the user's selection of user_type, and

在otherFile.php中检索表单数据。但我想为三个PHP文件中的一个执行此操作,具体取决于用户对user_type的选择,以及

<form method="post" action="otherFile.php|otherFile2.php|otherFile3.php">

clearly doesn't work.

显然不起作用。

3 个解决方案

#1


2  

You can use $_SESSION to store data for use in other areas of your site.

您可以使用$ _SESSION存储数据,以便在您网站的其他区域中使用。

To simplify it, you could do something like:

为了简化它,您可以执行以下操作:

session_start();
$_SESSION['firstname'] = $_POST['firstname']; // store firstname, lastname as sessions
$_SESSION['lastname'] = $_POST['lastname']; 

And then, inside of another page, you can call these sessions to output:

然后,在另一个页面内,您可以调用这些会话输出:

$firstname = $_SESSION['firstname'];
print("$firstname"); // will print the first name.

I hope this helps :)

我希望这有帮助 :)

EDIT: Before you create a $_SESSION and try to print it into another page, create a core.php page and include it into both page1.php & page2.php

编辑:在创建$ _SESSION并尝试将其打印到另一个页面之前,创建一个core.php页面并将其包含在page1.php和page2.php中

Inside of your core.php script, you need to start up a session, like so:

在你的core.php脚本里面,你需要启动一个会话,如下所示:

session_start();

Once you have included the core.php file into both of your pages, try printing out the desired $_SESSION variable again.

将core.php文件包含到两个页面后,请尝试再次打印出所需的$ _SESSION变量。

#2


0  

I'm not sure if this is you want, but you can create a single php, and if is a value, require it using include or require functions.

我不确定这是否是你想要的,但你可以创建一个php,如果是值,则需要使用include或require函数。

For example, processor.php

例如,processor.php

    <? 
$user_type = $_POST["user_type"];
switch($user_type) {
    case "a":
        require_once 'otherFile.php';
        break;
    case "b":
        require_once 'otherFile2.php';
        break;
    case "c":
        require_once 'otherFile3.php';
        break;
    default:
        die("Not allowed");
        break;
}
?>

#3


0  

Using javascript:

<script src="https://code.jquery.com/jquery-1.11.3.js"></script>
<script type="text/javascript">
jQuery("#myForm").submit(function( event ) {
    var radio = jQuery('input[name=user_type]:checked', this).val();
    if(radio == 'a')
        this.action = 'otherFile.php';
    else if(radio == 'b')
        this.action = 'otherFile2.php';
    else if(radio == 'c')
        this.action = 'otherFile3.php';
    else
        return false;
});
</script>

You form:

<form method="post" action="" id="myForm">
    <input type="radio" name="user_type" value="a" id="a">
    <label for="a">A</label>

    <input type="radio" name="user_type" value="b" id="b">
    <label for="b">B</label>

    <input type="radio" name="user_type" value="c" id="c">
    <label for="c">C</label>

    <input type="submit" value="Submit">
</form>

#1


2  

You can use $_SESSION to store data for use in other areas of your site.

您可以使用$ _SESSION存储数据,以便在您网站的其他区域中使用。

To simplify it, you could do something like:

为了简化它,您可以执行以下操作:

session_start();
$_SESSION['firstname'] = $_POST['firstname']; // store firstname, lastname as sessions
$_SESSION['lastname'] = $_POST['lastname']; 

And then, inside of another page, you can call these sessions to output:

然后,在另一个页面内,您可以调用这些会话输出:

$firstname = $_SESSION['firstname'];
print("$firstname"); // will print the first name.

I hope this helps :)

我希望这有帮助 :)

EDIT: Before you create a $_SESSION and try to print it into another page, create a core.php page and include it into both page1.php & page2.php

编辑:在创建$ _SESSION并尝试将其打印到另一个页面之前,创建一个core.php页面并将其包含在page1.php和page2.php中

Inside of your core.php script, you need to start up a session, like so:

在你的core.php脚本里面,你需要启动一个会话,如下所示:

session_start();

Once you have included the core.php file into both of your pages, try printing out the desired $_SESSION variable again.

将core.php文件包含到两个页面后,请尝试再次打印出所需的$ _SESSION变量。

#2


0  

I'm not sure if this is you want, but you can create a single php, and if is a value, require it using include or require functions.

我不确定这是否是你想要的,但你可以创建一个php,如果是值,则需要使用include或require函数。

For example, processor.php

例如,processor.php

    <? 
$user_type = $_POST["user_type"];
switch($user_type) {
    case "a":
        require_once 'otherFile.php';
        break;
    case "b":
        require_once 'otherFile2.php';
        break;
    case "c":
        require_once 'otherFile3.php';
        break;
    default:
        die("Not allowed");
        break;
}
?>

#3


0  

Using javascript:

<script src="https://code.jquery.com/jquery-1.11.3.js"></script>
<script type="text/javascript">
jQuery("#myForm").submit(function( event ) {
    var radio = jQuery('input[name=user_type]:checked', this).val();
    if(radio == 'a')
        this.action = 'otherFile.php';
    else if(radio == 'b')
        this.action = 'otherFile2.php';
    else if(radio == 'c')
        this.action = 'otherFile3.php';
    else
        return false;
});
</script>

You form:

<form method="post" action="" id="myForm">
    <input type="radio" name="user_type" value="a" id="a">
    <label for="a">A</label>

    <input type="radio" name="user_type" value="b" id="b">
    <label for="b">B</label>

    <input type="radio" name="user_type" value="c" id="c">
    <label for="c">C</label>

    <input type="submit" value="Submit">
</form>