douji1999 2014-04-15 11: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 11: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
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?