开发者

php - scope issues finding why var is empty on code excerpt

开发者 https://www.devze.com 2023-01-15 11:43 出处:网络
$roidInfo = \'\'; $nomeDominio =\'\'; if (isset($_GET[\'infoDominio\'])) { $nomeDominio = filter_input(INPUT_GET, \'nomeDominio\', FILTER_SANITIZE_STRING);
$roidInfo = '';
$nomeDominio ='';
   if (isset($_GET['infoDominio']))
   {
     $nomeDominio = filter_input(INPUT_GET, 'nomeDominio', FILTER_SANITIZE_STRING);
     $dominioVo->setNome($nomeDominio);
     try 
     {
       ...
      $roidInfo = isset($infoDominio['roid']) ? $infoDominio['roid'] : '';
     }
     catch (EppCommandsExceptions $e)
     {
       ...
     } 

     //all well here, I get the dump with values:
     //var_dump($nomeDominio);
     //var_dump($roidInfo);
    }
    if (isset($_POST['atualizarDominio']))
    {
      var_dump($nomeDominio); //dump ""
      var_dump($roidInfo); //dump ""

On the above excerpt I lost the value of $nomeDominio and $roidInfo but I don't understand why. Can I have your help to find out why is this happening?

UPDATE

Here is, the exact same issue, but by using SESSIONS:

<?php session_start(); ?>

<?php
 $roidInfo = '';
 $nomeDominio ='';

 if (isset($_GET['infoDominio']))
 {
    $nomeDominio = '123';
    try 
    {
      //...
      $roidInfo = '456';

      $_SESSION['nomeDominio'] = $nomeDominio;
      $_SESSION['roidInfo'] = $roidInfo;

      //retrieving the values. OK.
      var_dump($_SESSION['roidInfo']);
      var_d开发者_JAVA百科ump($_SESSION['nomeDominio']);
     }
     catch (EppCommandsExceptions $e)
     {
       //...
     }
 }

 if (isset($_POST['atualizarDominio']))
 {
    //retrieving index undefined. not ok.
    var_dump($_SESSION['nomeDominio']);
    var_dump($_SESSION['roidInfo']);


    //all the rest... runs
 }

?>

Thanks in advance, MEM


This was a weird case. I was able do pass the values due to my form action declarations.

I had, on the first form:

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

On the second form:

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

If we remove the action information from the second form:

<form action="" name="atualizarDominio" method="post">

It will work.

Thank you all that have replied, this is, à mon avis, a weird stuff. :s


Is isset($_GET['infoDominio'] actually true? If thats not the case $nomeDominio stays an empty string in the last code block.


You got two DIFFERENT conditions. one on $_GET['infoDominio'], one on $_POST['atualizarDominio']. Maybe only one of them is set and the other is not?

The "all well" code is run when $_GET['infoDominio'] is set. The "not all well" is run when $_POST['atualizarDominio'] is set. They are two different variables!

edit:

OK, as I found in the discussion under the original original question - the real issue is that PHP doesn't remember $nomeDominio and $roidInfo between each run of the script - you have to use sessions or cookies to remember them.

0

精彩评论

暂无评论...
验证码 换一张
取 消