...

View Full Version : Undefined Inex Error???



jjdave
07-18-2005, 11:02 PM
Hi All, 1st Timer...

I have created PHP page with Form as follows:
<?
$Fname = $_POST["Fname"];
$Lname = $_POST["Lname"];
?>

<form method="post" action="<?php echo $PHP_SELF;?>">
First Name:<input type="text" name="Fname"><br />
Last Name:<input type="text" name="Lname"><br />
<input type="submit" value="submit" name="submit">
</form>

& This is the error I am getting:
Notice: Undefined index: Fname in c:\testform.php on line 2
Notice: Undefined index: Lname in c:\testform.php on line 3

Actually its pretty straightforward, and yet I cant figure it out. Please help. Thanks.
jjdave...

delinear
07-18-2005, 11:06 PM
The error is telling you that the $_POST data with the names you are looking for doesn't exist - usually this means that either the field names are wrong on the form or the form wasn't submitted yet - from your code snippet it looks like the latter was the case.

It should be easy enough to get around the problem by checking the $_POST data with isset() first:

<?
if(isset($_POST['Fname']) {
$Fname = $_POST["Fname"];
}
if(isset($_POST['Lname']) {
$Lname = $_POST["Lname"];
}
?>

<form method="post" action="<?php echo $PHP_SELF;?>">
First Name:<input type="text" name="Fname"><br />
Last Name:<input type="text" name="Lname"><br />
<input type="submit" value="submit" name="submit">
</form>

jjdave
07-18-2005, 11:52 PM
Thank You for your reply: I am still having problem. Well here is my whole code: (Also, One more thing, How do you guys post your code, so it keeps its coding colors, such as red, blue, green and orange???)

<?
include 'library/config.php'; // Assign Parameters to Localhost, UserName, Password

if (!isset($_POST['Submit'])) // if page is not submitted to itself echo the form
{
$part_no=$_POST['part_no'];
$part_desc=$_POST['part_desc'];

include 'library/opendb.php'; // Connect to Database using Localhost, UserName, Password

$query="INSERT INTO xproject VALUES ('','$part_no','$part_desc')";

mysql_query($query); // Run the Query to create Table & Fields

include 'library/closedb.php'; // Disconnect from the Database
?>


<FORM Action="<?php echo $_SERVER['PHP_SELF']?>" Method="POST">

PART NO:<input type="text" name="part_no"><br>
DESCRIPTION:<input type="text" name="part_desc"><br>
<input type="submit" name="Submit" value="SUBMIT"><br>

</FORM>

<?
}
else
{
echo "Part No & Description ".$part_no." ".$part_desc.".<br />";
}
?>

==============================================
NOTE that this form does work, I am not sure why I am getting this error message:

Notice: Undefined index: part_no in c:\x1-testrecordadd\.php on line 6
Notice: Undefined index: part_desc in c:\x1-testrecordadd.php on line 7

Kurashu
07-19-2005, 12:26 AM
Use the PHP VbCode.

And here's your trouble maker:


$part_no=$_POST['part_no'];
$part_desc=$_POST['part_desc'];

You are assigning values that don't exist.

jjdave
07-19-2005, 03:25 PM
I moved all the Variables & SQL codes into the ELSE clause and now it works fine. Thanks.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum