Mohan Wijesena
Mohan Wijesena

Reputation: 21

How to resolve an 'Undefined Index' error?

Following is my code. I get an undefined index error when the validation is completed and about to populate the 'Success' message.

I have digged and identified where the problem is (<td><?= $errors['fname'] ?></td>) but what I don't understand is what is the correct method of defining the index which resulted the error. Can someone highlight where I've gone wrong? Thanks.

            <?php
        function VerifyForm(&$values, &$errors)
        {
        if($values['fname'] == "")
        {$errors['fname'] = 'empty';} 
        }
        function DisplayForm($values, $errors)
        {
        ?>
        <html>
        <head>
        <title>Register</title>
        </head>
        <body>
        <form action="<?= $_SERVER['PHP_SELF'] ?>" method="POST">
        <table border="1">
        <tr>
        <td>First Name :</td>
        <td><input type="text" name="fname" value="<?= htmlentities($values['fname']) ?>"/>
        <td><?= $errors['fname'] ?></td>
        </tr>
        <tr><td colspan="2" align="center"><input type="submit" value="Submit"></td></tr>
        </table>
        </form>
        </body>
        </html>
        <?php
        }
        function ProcessForm($values)
        {
        echo ("<p>Your record has been successfully added!</p>");
        }
        if ($_SERVER['REQUEST_METHOD'] == 'POST')
        {
        $formValues = $_POST;
        $formErrors = array();

        if (!VerifyForm($formValues, $formErrors))
        DisplayForm($formValues, $formErrors);
        else
        ProcessForm($formValues);
        }
        else
        DisplayForm(null, null);
        ?>

Upvotes: 0

Views: 407

Answers (3)

neythz
neythz

Reputation: 74

change this :

<td><?= $errors['fname'] ?></td>

to :

<td>
    <?php 
        if(isset($errors['fname'])){ 
            echo $errors['fname']; 
        } 
    ?>
</td>

Upvotes: 0

Fluffeh
Fluffeh

Reputation: 33502

You are setting form errors to an empty array:

$formValues = $_POST;
$formErrors = array();
// Did you mean to assign it some values before using it?

if (!VerifyForm($formValues, $formErrors))

So it has no element called ['fname'].

Reading through your code further, the following is happening:

You are sending the function an empty array (which is fine) but you are only filling it with info if there is an error - so far so good. However in your HTML code, you don't check to see if it contains anything (and if it all the info was posted there will be nothing) but you still try to access any errors in it.

You might want to change this line of code:

<td><?= $errors['fname'] ?></td>

To something like this:

if(!empty($errors))
{
    foreach($errors as $key => $val)
    {
        echo "<td>".$key." - ".$val."</td>";
    }
}

This will loop through your errors array and print out a <td> for every error you have - you could modify it to show a row for each error if needed.

Upvotes: 3

Yogesh Suthar
Yogesh Suthar

Reputation: 30488

use isset for this

<?php if(isset($errors['fname'])) { echo $errors['fname']; } ?>

this will solve your problem

Upvotes: 0

Related Questions