Solved

Undefined index in php

Posted on 2014-04-28
4
489 Views
Last Modified: 2014-04-28
I'm inexperienced with php, and I attempted to create a contact form on my website, which sends a visitor's message to me via Email.  I've tested the form, and it appears to be working.  Still,  the server log reports errors in the form handler php.  

Even though it seems to be working, I'd like to fix these errors if I could get some guidance.  I've attached the contact form, the form handler php and the relevant excerpt from the error log.

 Document.docrez-contact-form-handler.phprez-contact.html
0
Comment
Question by:ddantes
  • 2
  • 2
4 Comments
 
LVL 82

Accepted Solution

by:
hielo earned 250 total points
ID: 40028728
If you type the url to the form handler (http://www.mauitradewinds.com/RezEasy/rez_contact-form-handler.php) directly in your browser's address bar, then you should see the error messages you are describing.

When you type in the url directly, you are not POSTing anything.  Thus $_POST['whatever'] will generate the error you have described.  Try:

<?php 
$errors = '';
$myemail = 'stay@mauitradewinds.com';//<-----Put Your email address here.
if(empty($_POST['firstname'])  || 
   empty($_POST['lastname'])  ||
   empty($_POST['email']) || 
   empty($_POST['message']))
{
    $errors .= "\n Error: At a minimum, we need your name, Email address and message in order to transmit your form.";
}
else
{
    $firstname = array_key_exists('firstname',$_POST) ? $_POST['firstname']:''; 
    $lastname = array_key_exists('lastname',$_POST) ? $_POST['lastname']:''; 
    $email_address = array_key_exists('email',$_POST) ? $_POST['email']:''; 
    $message = array_key_exists('message',$_POST) ? $_POST['message']:''; 
    
    if (!preg_match(
    '/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i', 
    $email_address))
    {
        $errors .= "\n Error: Invalid email address";
    }
}

if( empty($errors))
{
	$to = $myemail; 
	$email_subject = "Rez-contact form";
	$email_body = "You have received a contact form from RezEasy. ".
	" Here are the details:\n First Name: $firstname \n Last Name: $lastname \n Email: $email_address \n Message: \n $message"; 
	
	$headers = "From: $myemail\n"; 
	$headers .= "Reply-To: $email_address";
	
	mail($to,$email_subject,$email_body,$headers);
	//redirect to the 'thank you' page
	header('Location: reply.html');
	exit;//You should always "exit" immediately after a redirection request
} 
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html>
<head>
	<title>Contact form handler</title>
</head>

<body>
<!-- This page is displayed only if there is some error -->
<?php
echo nl2br($errors);
?>


</body>
</html>

Open in new window

0
 

Author Comment

by:ddantes
ID: 40028759
Thank you for being specific with the code!

If I type the URL for the contact form handler, I get this in my browser:

Error: At a minimum, we need your name, Email address and message in order to transmit your form.
 Error: Invalid email address

This leaves me a little confused.  Are there errors in the form handler, or are there errors because a visitor fails to complete certain form fields?

In any case, I'm replacing the form handler with your code and testing again.
0
 
LVL 82

Expert Comment

by:hielo
ID: 40028765
>>If I type the URL for the contact form handler, I get this in my browser:
Correct.  The logic in your original code would also produce the message you just posted, but it would also generate an error in your error log.

With the code I posted, if there is nothing POSTed (meaning if you typed the url directly), you would still see the error, but you should not be seeing anything in the error log.

BTW, I anyone were to go to http://www.mauitradewinds.com/RezEasy/, from there, if they click on your form handler, then that is equivalent to NOT submitting a POST request.  Meaning that with your original code, going to the above url and clicking on your form handler would also reflect an error in your error log.

>>This leaves me a little confused.  Are there errors in the form handler,
Yes.  The reason for the entries in the error log is that this:
$firstname = $_POST['firstname'];
 assumes that there IS a key named "firstname" in $_POST.  Once again, if someone arrives at your page by typing the url (or clicking on a url that links directly to your form handler) then $_POST is empty  -- meaning that there is no such thing as $_POST['whatever'], but your code "blindly" assumes that there is.  Read the PHP manual about array_key_exists()

>> or are there errors because a visitor fails to complete certain form fields?
Well, if someone types the url directly (or clicks on a link that takes you directly to the form handler), then in essence the user failed to "complete" the required fields.  So the errors being reported are correct.
0
 

Author Comment

by:ddantes
ID: 40028787
Thank you for explaining.  I don't have any links to the form handler on my site, so I don't know how visitors would have bypassed the form itself, and landed on the form handler page.  At any rate, your code works fine, and the log isn't reporting the errors anymore.  Much appreciated.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now