Solved

Undefined index in php

Posted on 2014-04-28
4
502 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

828 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