gpt4 book ai didi

javascript - 将 PHP 文件中的 HTML 表单数据传递到其他 3 个 PHP 文件之一

转载 作者:行者123 更新时间:2023-11-27 22:53:39 24 4
gpt4 key购买 nike

我正在构建一个具有简单登录界面的网站。用户可以使用 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>

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

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

在登录页面中指向另一个文件。并使用

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

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

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

显然不行。

最佳答案

您可以使用 $_SESSION 存储数据以供网站其他区域使用。

为了简化它,你可以这样做:

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

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

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

我希望这有帮助:)

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

core.php 脚本内,您需要启动一个 session ,如下所示:

session_start();

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

关于javascript - 将 PHP 文件中的 HTML 表单数据传递到其他 3 个 PHP 文件之一,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37825929/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com