Current location: Hot Scripts Forums » Programming Languages » PHP » PHP Notice: Undefined index


PHP Notice: Undefined index

Reply
  #1 (permalink)  
Old 08-18-08, 03:28 PM
pher pher is offline
Newbie Coder
 
Join Date: Aug 2008
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
PHP Notice: Undefined index

Hi all,

I'm new to PHP. I wrote a basis PHP script to send data from a web form into an email. The script is working fine and able to send the data into the specific email address. However, after you click on the submit button, the PHP Notice: Undefined index log display. Anyone has any idea how to get this issue resolved.
Reply With Quote
  #2 (permalink)  
Old 08-18-08, 03:37 PM
pher pher is offline
Newbie Coder
 
Join Date: Aug 2008
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
PHP Notice: Undefined index

Attached is my PHP Script.

Hi all,

I'm new to PHP. I wrote a basis PHP script to send data from a web form into an email. The script is working fine and able to send the data into the specific email address. However, after you click on the submit button, the PHP Notice: Undefined index log display. Anyone has any idea how to get this issue resolved.[/QUOTE]
Reply With Quote
  #3 (permalink)  
Old 08-18-08, 04:58 PM
alphapatrol alphapatrol is offline
Newbie Coder
 
Join Date: Jul 2008
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Could you paste your script here?
__________________
AlphaPatrol
network monitoring
Security Tools and Services
Reply With Quote
  #4 (permalink)  
Old 08-19-08, 12:16 AM
<?Wille?> <?Wille?> is offline
Junior Code Guru
 
Join Date: Jan 2004
Location: Helsinki, Finland
Posts: 666
Thanks: 0
Thanked 0 Times in 0 Posts
Cant see any attachments.

Anyway, "undefined index" means just that, your trying to access a array element named "log display" which doesnt exist. You can make the error disappear by defining a default value for the variable
Reply With Quote
  #5 (permalink)  
Old 08-27-08, 01:51 PM
pher pher is offline
Newbie Coder
 
Join Date: Aug 2008
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Hi all,

Sorry for the delay in responding. Anyways, thanks for all of your tried to help. I did find the solution to my problem. Below is my PHP script with the solution. Error_reporting(E_ALL^E_NOTICE); is the answer to the undefined index.

PHP Code:

<?php 

error_reporting
(E_ALL E_NOTICE);

$to "pher@anokatech.edu"
$subject "Librarian Form"

$FirstName $_REQUEST['FirstName'] ;
$LastName $_REQUEST['LastName'] ;
$Email $_REQUEST['Email'] ; 
$WorkPhone $_REQUEST['WorkPhone'] ;
$HomePhone $_REQUEST['HomePhone'] ;
$Question $_REQUEST['Question'] ;
$SourcesChecked $_REQUEST['SourcesChecked'] ;
$Contact $_REQUEST['Contact'] ;
 
$body " First Name: $FirstName \n\n Last Name: $LastName \n\n Email: $Email \n\n Work Phone: $WorkPhone \n\n Home Phone: $HomePhone \n\n Question: $Question \n\n Sources Checked: $SourcesChecked \n\n Contact: $Contact \n\n";

$headers "From: $Email"
$sent mail($to$subject$body$headers) ; 
if(
$sent
{print 
"Your Request was sent successfully."; }
else 
{print 
"We encountered an error sending your request."; }
?>

Last edited by mab; 08-27-08 at 01:58 PM. Reason: Code in code tags please.
Reply With Quote
  #6 (permalink)  
Old 08-27-08, 02:04 PM
mab's Avatar
mab mab is offline
Community VIP
 
Join Date: Oct 2005
Location: Denver, Co. USA
Posts: 2,674
Thanks: 0
Thanked 0 Times in 0 Posts
Disabling notice messages does not actually fix anything. The error is still occurring, causing php to take 10 to 20 times longer to execute the line of code where the error is.

If you post the actual error message, someone can help you find the cause.

I am going to assume this is purely a learning exercise as the posted code is not validating the input from the form and would allow a hacker or a spam bot script to send anything to anyone.
__________________
Error checking, error reporting, and error recovery. If your code does not have these to get it to tell you why it is not working, what makes you think someone in a programming forum will be able to tell you why it is not working???
Reply With Quote
  #7 (permalink)  
Old 08-28-08, 08:49 AM
End User's Avatar
End User End User is offline
Level II Curmudgeon
 
Join Date: Dec 2004
Posts: 3,027
Thanks: 14
Thanked 35 Times in 33 Posts
Quote:
Originally Posted by pher View Post
I did find the solution to my problem. Below is my PHP script with the solution. Error_reporting(E_ALL^E_NOTICE); is the answer to the undefined index.
Actually all E_ALL^E_NOTICE does suppress the printing of the error message. As mab mentioned it doesn't fix anything. It's like putting a band-aid on a cut- the cut is still there, you just can't see it. You're better off fixing the actual problem by simply assigning a default value to the variable.
__________________
I don't live on the edge, but sometimes I go there to visit.
-------------------------------------------------------------------------
Sanitize Your Data | Oracle Date & Substring Functions | Code Snippet Library | [url=http://www.codmb.com/Call Of Duty[/url]
Reply With Quote
  #8 (permalink)  
Old 08-28-08, 01:52 PM
pher pher is offline
Newbie Coder
 
Join Date: Aug 2008
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
If I took the Error_reporting(E_ALL^E_NOTICE) out of the script, then following message display. However, I did get the data fill on the form email to me as I expected. The following message display only when at least one field was not filled in. Otherwise, the script is working.

PHP Notice: Undefined index: FirstName in D:\ATCWeb\scripts\librarianform.php on line 7 PHP Notice: Undefined index: LastName in D:\ATCWeb\scripts\librarianform.php on line 8 PHP Notice: Undefined index: Email in D:\ATCWeb\scripts\librarianform.php on line 9 PHP Notice: Undefined index: WorkPhone in D:\ATCWeb\scripts\librarianform.php on line 10 PHP Notice: Undefined index: HomePhone in D:\ATCWeb\scripts\librarianform.php on line 11 PHP Notice: Undefined index: Question in D:\ATCWeb\scripts\librarianform.php on line 12 PHP Notice: Undefined index: SourcesChecked in D:\ATCWeb\scripts\librarianform.php on line 13 PHP Notice: Undefined index: Contact in D:\ATCWeb\scripts\librarianform.php on line 14
Reply With Quote
  #9 (permalink)  
Old 08-28-08, 08:36 PM
loveloop loveloop is offline
Newbie Coder
 
Join Date: Aug 2008
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
put this 'error_reporting(0)" into your files
Reply With Quote
  #10 (permalink)  
Old 08-29-08, 02:15 AM
UnrealEd's Avatar
UnrealEd UnrealEd is offline
Community Liaison
 
Join Date: May 2005
Location: Antwerp, Belgium
Posts: 3,165
Thanks: 4
Thanked 25 Times in 25 Posts
NO: putting that on top of your code doesn't solve the problems, it hides the problems.

The problem is pretty straightforward: you're trying to access elements of the $_REQUEST array, but they don't exist yet. Here's an example:
PHP Code:

$myArray = array ();

$myArray['abc'] = 123;
$myArray['def'] = "hello world";

echo 
$myArray['abc']; // will output 123
echo $myArray['def']; // will output "hello world"

echo $myArray['ghi']; // will throw a "undefined index" notice 
__________________
"Good judgement comes from experience, and experience comes from bad judgement." - Fred Brooks

Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP and MySQL ? rob2132 Hot Scripts Forum Questions, Suggestions and Feedback 4 08-29-08 02:22 AM
2 profitable script sites for sale cms-master.com General Advertisements 3 07-03-07 10:17 AM
Php Mysql Bug??? tranquilraven PHP 4 03-01-06 03:06 AM
NOTICE: Undefined index jozin PHP 6 04-29-05 04:19 AM
Notice: Undefined variable: netbakers PHP 2 01-17-05 08:03 AM


All times are GMT -5. The time now is 07:53 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.