Jump to content

Don't understand : Undefined index errors.


TrotskyIcepick

Recommended Posts

I am testing a php form validation script I found on the internet and it is giving undefined index errors on all form input fields, for example :

 

Notice: Undefined index: user_name in C:\www\drupal6\iris\demo.php on line 172

 

the code here is :

 

<input type="text" name="user_name" value="<?=$fields['user_name']?>" />

 

This code works fine on my Linux machine but not on Windows with php 5.3.3.

 

I have attached the code for you to examine.

 

Thanks in Advance

Andrew

 

[attachment deleted by admin]

Link to comment
https://forums.phpfreaks.com/topic/222215-dont-understand-undefined-index-errors/
Share on other sites

The Linux/Windows machines will have different levels of error_reporting.

 

All the notice means is that you're trying to use an index within an array that doesn't exist. i.e. $fields['user_name'] doesn't exist.

 

To rectify you need to either explicitly set a value (even if it's an empty string) for $fields['username'] earlier within the code, or check that the index exists before you try to echo it:

 

<input type="text" name="user_name" value="<?php if (isset($fields['user_name'])) echo $fields['user_name']; ?>" />

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.