Undefined index. Server Error or coding problem?
i tried to do some practise about form actions and inputs and copy-paste a piece of codes but i got Undefined index: fname and Undefined index: age. The cod开发者_开发问答es were taken from very popular php tutorial website, there should not be any problem with them but submitting them anyway.Is this server error or something like that?
<form action="posting.php" method="post">
Name: <input type="text" name="fname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
<?php
echo $_POST["fname"];
echo $_POST["age"];
?>
You need to make sure $_POST
is getting information before you try to read from it. There are a variety of ways to do this. Here's one:
<form action="posting.php" method="post">
Name: <input type="text" name="fname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
<?php
if(isset($_POST)) {
echo $_POST["fname"];
echo $_POST["age"];
}
?>
Until that form is submitted, $_POST
is empty and trying to read anything out of it will result in such a notice.
<?php
if (isset($_POST['fname']) && isset($_POST['age'])) {
echo $_POST['fname'];
echo $_POST['age'];
}
精彩评论