【问题标题】:scope of $_POST[] , trying to access $_POST[] variable, getting the undefined index error$_POST[] 的范围,试图访问 $_POST[] 变量,得到未定义的索引错误
【发布时间】:2015-04-28 20:11:51
【问题描述】:

我有这两个表单提交到同一脚本的不同块。我无法访问另一个块中一个块的变量,尽管两个变量都在同一个脚本中。

html表单:(1.php)

<html>
  <form method="POST" action="2.php" enctype="multipart/form-data">
    </br>
    Choose a user name:</font>
    <input type="text" name="username">
    <input type="submit" name="submit" value="Save and Proceed">
  </form>
</html>

2.php:

 <?php
    $name=$_POST['username'];
    if ((isset($_POST['username'])) && ($_POST['submit'] == 'Save and Proceed'))
    {
        $name=$_POST['username'];
        echo $name;
        if($name=='azra')
        {
        ?>
        <html>
          <form method="POST" action="2.php" enctype="multipart/form-data"></br>
            enter age:</font>
            <input type="text" name="age">
            <input type="submit"  name="submit" value="done">
          </form>
        </html>
        <?php
        }
    }

    if((isset($_POST['age'])) && ($_POST['submit'] == 'done'))
    {
        $age=$_POST['age'];
        echo $age;
        if($age==25)
        {
            echo "hi" .$name;
            echo "your age is ". $age;
            echo"you are eligible";
        }
    }   

  ?>

如何在同一脚本中的 html 表单后面的代码中访问 $_POST['username']?提前谢谢你。

【问题讨论】:

  • 你可以使用&lt;?php echo $_POST['username'] ?&gt;,我真的不确定你需要做什么??
  • 旁注:您的 HTML 无效。您有一个结束 &lt;/font&gt; 标记,但没有一个开始 &lt;font&gt; 标记。
  • 这是一个拼写错误对不起

标签: php html forms post scope


【解决方案1】:

在第二种形式中,您可以使用hidden input,即:

<input type="hidden" name="username" value="$name">

例子:

<?php

    $name=$_POST['username'];
    if (!empty($_POST['username']) && $_POST['submit'] == 'Save and Proceed'))
    {
        $name=$_POST['username'];
        echo $name;
        if($name=='azra')
        {
            echo <<< LOL
         <html>
        <form method="POST" action="2.php" enctype="multipart/form-data"></br>
        enter age:</font> <input type="text" name="age">
         <input type="hidden" name="username" value="$name">
        <input type="submit"  name="submit" value="done">
        </form>
        </html>
LOL;
        }
    }

    if((isset($_POST['age'])) && ($_POST['submit'] == 'done'))
    {
        $age=$_POST['age'];
        echo $age;
        if($age==25)
        {
            echo "hi" .$name;
            echo "your age is ". $age;
            echo"you are eligible";
        }
    }   

?>

【讨论】:

  • 我喜欢 heredoc 标识符。 +1
  • 我一直用它,我不喜欢转义引号,双引号,太浪费时间了,+1 to u 2 ;)
【解决方案2】:

如果我理解你想传递用户名两次。 比你可以使用隐藏的输入,这是不可见的(只传输数据):

<input type="hidden" name="username" value="<?php echo $_POST['username']; ?>" />

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-11
    • 1970-01-01
    • 2018-03-05
    • 2020-01-17
    • 1970-01-01
    • 2018-12-15
    • 2014-12-20
    相关资源
    最近更新 更多