【问题标题】:How process two forms in 1 PHP file?如何处理 1 个 PHP 文件中的两个表单?
【发布时间】:2015-03-30 17:36:38
【问题描述】:

我有 1 个单独的 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>

【问题讨论】:

  • 想给出一个代码示例吗?
  • 如果您费心阅读我链接的整个页面,您将在那里看到代码示例!
  • 你有预付款吗?记得检查一个答案是否正确;-)
  • 没有一个答案是真正正确的,但你的回答对我的会话有所帮助。

标签: php html variables post persistence


【解决方案1】:

等到表单完成再提交怎么样?我会使用一些 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>

【讨论】:

    【解决方案2】:

    只需在您的输入类型中设置正确的信息隐藏,当您发送第二个表单时,您可以在$_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,看看这个例子:

    How preserve my inputs values each time that I refresh my page?

    【讨论】:

      猜你喜欢
      • 2014-05-29
      • 2021-04-22
      • 2010-09-28
      • 1970-01-01
      • 1970-01-01
      • 2013-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多