如何在一个PHP文件中处理两个表单


How process two forms in 1 PHP file?

我有一个PHP文件,其中包含2个变量。。。我想要实现的是,首先从用户那里获得名字,并存储在变量$name中,然后显示一个表单,询问用户的姓氏,然后。。打印两个变量,问题是,当提交第二个表单时,第一个变量消失了,有办法把它保存在内存中吗?

<?php
$title = rand(100, 300);
?>
<!doctype html>
<html>
<head>
<?php
//functions to call
function nameform() {
?>
<center><form action="<?php
echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="text" autofocus name="name"><br>
<input type="submit" value="name">
<input type="hidden" name="val1">
</form></center>
<?php
}
function lastname() {
?>
<center><form action="<?php
echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="text" autofocus name="lastname"><br>
<input type="submit" value="lastname">
<input type="hidden" name="val2">
</form></center>
<?php
}
?>
<meta charset="utf-8">
<title><?php
echo ("$title"); ?></title>
</head>
<body>
<?php
if(!isset($_POST['name'])){
nameform();
}
if (isset($_POST['name'])) {
    $name = $_POST['name'];
    lastname();
}
if (isset($_POST['lastname'])) {
    $lastname = $_POST['lastname'];
    echo "Your name is $name and your last name is $lastname";
}
?>
</form>
</body>
</html>

等到表单完成后再提交如何?我会用一点javascript来处理这个问题。

<?php
$title = rand(100, 300);
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title><?php
    echo ("$title"); ?></title>
</head>
<body>
<center>
<?php
if (isset($_POST)) {
    $name       = $_POST['name'];
    $lastname   = $_POST['lastname'];
    echo "Your name is $name and your last name is $lastname";
} else {
?>
<form action="<?php
echo $_SERVER['PHP_SELF']; ?>" method="post">
    <div id="firstname">
        <input type="text" autofocus name="name"><br>
        <a href="javascript:;" 
onclick="document.getElementById('firstname').style.visibility='hidden';document.getElementById('lastname').style.visibility='visible';">Next</a>
    </div>
    <div id="lastname" style="display:none;">
        <input type="text" name="lastname"><br>
        <input type="submit" value="lastname">
    </div>
</form>
<?php } ?>
</center>
</body>
 </html>

只需在输入类型中设置隐藏的正确信息,当发送第二个表单时,就可以在$_POST['val2']中获得名称值。

参见示例:

<?php
$title = rand(100, 300);
?>
<!doctype html>
<html>
<head>
<?php
//functions to call
function nameform() {
?>
<center><form action="<?php
echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="text" autofocus name="name"><br>
<input type="submit" value="name">
<input type="hidden" name="val1">
</form></center>
<?php
}
function lastname() {
?>
<center><form action="<?php
echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="text" autofocus name="lastname"><br>
<input type="submit" value="lastname">
<input type="hidden" name="val2" value="<?php echo $_POST['name']; ?>">
</form></center>
<?php
}
?>
<meta charset="utf-8">
<title><?php
echo ("$title"); ?></title>
</head>
<body>
<?php
if(!isset($_POST['name'])){
nameform();
}
if (isset($_POST['name'])) {
    $name = $_POST['name'];
    lastname();
}
if (isset($_POST['lastname'])) {
    $lastname = $_POST['lastname'];
/*
Here you can get the name that come from the input type hidden named "val2"
*/
        $name = $_POST['val2'];
        echo "Your name is $name and your last name is $lastname";
    }
    ?>
    </form>
    </body>
    </html>

或者,正如sjagr所说,你可以使用SESSIONS,看看这个例子:

如何在每次刷新页面时保留输入值?