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
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']; ?>" />

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.