Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Undefined index in php

Posted on 2014-04-28
4
Medium Priority
?
507 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 82

Accepted Solution

by:
hielo earned 1000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

670 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