down00111 2010-09-08 15:01
浏览 52
已采纳

将值从一种形式传递到另一种形式 - 一种奇怪的情况

Please have a look to the following code:

<?php

  $nomeDominio='';

  if (isset($_GET['infoDominio']))
  {          
      $nomeDominio = $_GET['nomeDominio'];
      echo "I'm getting ".$nomeDominio;
  }

  if (isset($_POST['atualizarDominio']))
  {
      echo "I'm posting ".$nomeDominio;
  }

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>

    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Test Case 99</title>
    </head>

    <body>

        <form name="infoDominio" action="<?php echo htmlentities($_SERVER['PHP_SELF']);?>"  method="get">

            <input id="nome_dominio" type="text" name="nomeDominio" value="<?php echo $nomeDominio; ?>"/>
            <br />
            <button name="infoDominio" type="submit">Obtem informacao</button>

        </form>

        <form action="<?php echo htmlentities($_SERVER['PHP_SELF']);?>" name="atualizarDominio" method="post">

            <input type="hidden" value="<?php echo $nomeDominio ?>" name="nome-dominio"/>
            <br />
            <button type="submit" name="atualizarDominio">atualizar domínio</button>

        </form>

    </body>

</html>

You can copy/paste - it will serve as test case.

Like this, IF we get and then we post: The value from GET WILL NOT pass into POST.

The thing is: If we just change the action= property of the second form element to, instead of having the $_SERVER['PHP_SELF'], to have just action=""; you will notice that the value WILL pass.

My question is: Why?

ADDITIONAL NOTE: This is not something to solve. Instead, this is something to understand why is it happening this way. Why, if we change the action on the second form to action="", the value stored in $nomeDominio pass from one conditional into another? The code sample can be used by itself, so you can perfectly test this very easily and see what I'm talking about.

  • 写回答

4条回答 默认 最新

  • dongmin3754 2010-09-08 15:06
    关注
     {          
          $nomeDominio = $_GET['nomeDominio'];
          echo "I'm getting ".$nomeDominio;
      }
    
      if (isset($_POST['atualizarDominio']))
      {
          $nomeDominio = $_POST['nomeDominio']; //THIS HERE
          echo "I'm posting ".$nomeDominio;
      }
    

    you are missing the line with comment THIS HERE

    You wanted to pass the _GET['nomeDominio'] from the first form to a hidden field of the second form right? Then when we submit the SECOND form you echo nomeDominio's value again (from the second form's hidden field). You where missing and assignement in the $_POST: $nomeDominio = $_POST['nomeDominio'];

    There you go. If you do not undesrtand I do not know how to say differently.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算