我有一个不知道如何解决的问题,如果可能的话,我希望有更多了解 html/php 的人可以帮助我。 我正在为学校项目创建一个表单,我需要将表单的值传递到页面以发送到数据库,但我不知道如何执行此操作并将文件保留在同一页面上。
我到处寻找我认为可以给我答案的地方,但没有任何东西真正帮助我。
There are several ways to pass values from one page to another in PHP:
Using query strings: You can append values to the URL as query strings and retrieve them on the next page using the $_GET superglobal. For example: Page 1:
$value = "123";
header("Location: page2.php?value=$value");
Page 2:
$value = $_GET['value'];
echo $value;
Using sessions: You can store values in session variables and access them on the next page. For example: Page 1:
session_start();
$_SESSION['value'] = "123";
header("Location: page2.php");
Page 2:
session_start();
$value = $_SESSION['value'];
echo $value;
Using cookies: You can store values in cookies and retrieve them on the next page. For example: Page 1:
setcookie("value", "123", time() + 3600);
header("Location: page2.php");
Page 2:
$value = $_COOKIE['value'];
echo $value;
Using POST method: You can pass values using a form with the POST method and retrieve them on the next page using the $_POST superglobal. For example: Page 1:
<form action="page2.php" method="post">
<input type="hidden" name="value" value="123">
<input type="submit" value="Submit">
</form>
Page 2:
$value = $_POST['value'];
echo $value;
These are some common ways to pass values from one page to another in PHP. Choose the one that best fits your requirements.