douji1999 2014-04-15 19:24
浏览 137
已采纳

未定义的索引有POST问题

When i first open the site in wamp it says:

Notice: Undefined index: name in C:\wamp\www\fshije\fshije.php on line 11

Notice: Undefined index: email in C:\wamp\www\fshije\fshije.php on line 12.

But after i put some values it works and click submit the Notice table will disapear.

I could solve this if i would write inside action"anotherfile.php" and the 2 last lines of PHP at that other file normally with post changed to GET can any one explain me how to avoid the Notice tables at the start without the second option.

I am sory that i aksed this question i am new in php

Thank You

<!DOCTYPE html>
<html>
    <body>
        <?php
            echo'<form method="POST" action="">';
            echo 'Name: <input type="text" name="name"><br>';
            echo 'E-mail: <input type="text" name="email"><br>';
            echo '<input type="submit">';
            echo '</form>';

            echo $_POST['name']; echo "<br>";
            echo $_POST['email'];
        ?>
    </body>
</html>
  • 写回答

1条回答 默认 最新

  • douxu3315 2014-04-15 19:26
    关注

    $_POST[] won't contain anything until after the form is POSTed, use an isset() conditional.

    if(isset($_POST['name'], $_POST['email'])) {
        echo $_POST['name']; echo "<br>";
        echo $_POST['email'];
    }
    

    As a side note, I wouldn't show any HTML if you are processing the form (unless you have an error to display with the form). In this case you would continue the conditional like this:

    } else {
         // Display HTML and form here
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题