dougongnan2167 2012-12-27 18:04
浏览 52
已采纳

在浏览器刷新后尝试保留在PHP表单文本字段中输入的值时出错

I am trying run a small PHP script. I get an error when I try to run this small PHP line: <?PHP print $firstname ; ?> within the html code. According to my tutorial guide this line in the value element should ensure that I keep any input text entered into the text fields even after I refresh the browser, but it does not! the following error message occurs:

Notice: Undefined variable: surname in C:\wamp\www\pages\basicForm.php on line 27 Call Stack #TimeMemoryFunctionLocation 10.0007366504{main}( )..\basicForm.php:0 " NAME="surname">

Why?

and the code:

<!DOCTYPE html>
<html>
<head>
    <title></title>
        <?PHP
        if (!empty($_POST)) {
            $firstname = $_POST['firstname'];
            $surname = $_POST['surname'];
            print($firstname);
            print($surname);
        }
        else {

        } print( 'welcomes');
    ?>
</head>
<body>
<Form name ="form1" Method ="POST" Action ="basicForm.php">
    <label>


    <INPUT TYPE = 'TEXT' Name ='firstname' VALUE="<?PHP print $firstname ; ?>">
    </label>

    <p>
        <label>
            <INPUT TYPE="TEXT" VALUE="<?PHP print($surname); ?>" NAME="surname">
        </label>

    <p>
        <Input Type = "Submit" Name = "Submit1" Value = "Login">
</FORM>


</body>
</html>
  • 写回答

4条回答 默认 最新

  • douhuifen9942 2012-12-27 18:08
    关注

    All you need to assign initial value as below

    $firstname = '';
    $surname = '';
    

    Relying on the default value of an uninitialized variable is problematic in the case of including one file into another which uses the same variable name. It is also a major security risk with register_globals turned on. E_NOTICE level error is issued in case of working with uninitialized variables, however not in the case of appending elements to the uninitialized array. isset() language construct can be used to detect if a variable has been already initialized.

    Read more in detail: PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset"

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

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?