开发者

form post wipes out a file-scoped variable

When I POST a form to the same .php file a file-scope variable is null when it shouldn't be.

includefile.php:

 <?php

     $foo = " ";
 ?>

doIt.php:

<?php

    echo $foo;

echo <<<_END
  <form action="doIt.php" method="post"><pre>
    $nameLabel  : <input type="text" name=$nameLabel />
    <input type="submit" name="addrecord" value="ADD RECORD" />

_END;

index.php

<?php
   require_once 'includefile.php';

   $foo = "Set now.";  

   require_once 'doIt.php';
?>

The first time it loads, index.php causes $foo to echo and it says "Set now." But when I press the submit button on the form -- $foo is empty. Why does re-entry into doIt.php kill the value of $foo? NOTE: the require_once changed nothing -- still same problem.

My guess is that the form POST and the resulting re-entry into the same .php file set开发者_开发知识库s up a new call frame on the stack with everything set to nothing.


It looks like your form needs to be submitted to 'index.php' and not 'doit.php'. In the 'doit.php' file, $foo is set to empty string from the require_once file, and is never set through 'index.php' as you might have expected. So:

<form action="index.php" method="post">

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜