Wednesday, March 25, 2009

Notice: Undefined index: (php)

Notice: Undefined index:
Sometimes you get this notice (This is not a error). lets see a example why you get this notice or warning.



echo $name= $_POST["name"];
echo $number= $_POST["number"];


<form name="form1" method="POST" action="">
<table border="0">
<td>Name :</td>
<td> <input type="text" name="name" /></td>
<td>Tel: Number :</td>
<td> <input type="text" name="number" /></td>
<td> </td>
<td><input type="submit" name="sub" value="Add" /></td>

When you initially load this page you will get a notice like following.
Undefined index

This notice is shown because your php.ini file has enabled Notices to display or you may have a set error_reporting() function to E_ALL or to display notices.

The reason for the notice because when the page is load it search for the $_POST["name"] and $_POST["number"] which actually not available, $_POST indexes are set when you submit the form not when you just load the page.

1) you can fix the error reporting levels in php.ini or by error_reporting() function.


Change the code like this.

echo $name= $_POST["name"];
echo $number= $_POST["number"];

This will make the code run only when you submit the add button.

No comments:

Post a Comment