Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 511
  • Last Modified:

Undefined index in php

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
ddantes
Asked:
ddantes
  • 2
  • 2
1 Solution
 
hieloCommented:
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
 
ddantesAuthor Commented:
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
 
hieloCommented:
>>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
 
ddantesAuthor Commented:
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now