0

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);
        ?>
3
  • the error means $error['fname'] is not defined. You should initialize it to get rid of the error message. Commented Sep 28, 2012 at 7:57
  • Thanks wroniasty. can you elaborate a bit more on the initialization part. Commented Sep 28, 2012 at 8:09
  • @MohanWijesena is it your code?? Commented Sep 28, 2012 at 8:11

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

@MohanWijesena See edit, I gave you some extra code/information.
0

use isset for this

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

this will solve your problem

Comments

0

change this :

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

to :

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.