Contact-form-handler.php stopped working on my sites

I maintain a couple of websites (my own and a non-profit) that has a contact/donation form. This form was obtained many years ago as a free form. It has stopped working and I am trying to fix it but don’t know PHP. Can I copy the script here so someone can look at it and help me fix it?

Sure you can copy the code here. When you post code in the forum, you need to format it. To do so you can either select all the code and click the </> button, or type 3 backticks ``` on a separate line both before and after the block of code.

We will also need to know what error(s) you are getting when someone submits a form. If nothing is showing in the browser, you should look in the error logs. They should be available from your control panel.

It would also be handy to know what version of PHP your server is running.

Do you know what changed between when it did work, then didn’t?
If it’s an old script, perhaps the host stopped providing an old PHP version and the script is no longer compatible. It may not be that, just the first idea that came to mind.
But the error log is often the key to finding what’s wrong.

Here is the code for the donation form. I am not clear on when this stopped working. My client only recently contacted me saying visitors to her site could not use the form. After filling out the form and clicking the submit button, it takes me to a page that says this page isn’t working. It is currently unable to handle this request. Her site is hosted at godaddy.com and is using 8.1 PHP.

<?php 
$errors = '';
$myemail = 'info@abc.org';
if(empty($_POST['name'])  || 
   empty($_POST['email'])) 
{
    $errors .= "\r\n Error: all fields are required";
}

$name = $_POST['name']; 
$address = $_POST['address'];
$city = $_POST['city'];
$state = $_POST['state'];
$zipcode = $_POST['zipcode'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$memory = $_POST['memory'];
$honor = $_POST['honor'];
$checkbox = $_POST['checkbox'];


if (!eregi(
"^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", 
$email))
{
    $errors .= "\r\n Error: Invalid email address";
}
if( empty($errors))
{
	$to = $myemail; 
	$email_subject = "Wings of Hope for Pancreatic Cancer Research Donation: $name";
	$email_body = "You have received a new donation. ".
	" Here are the details:\r\n Name: $name\r\n Address: $address\r\n City: $city\r\n State: $state\r\n Zip Code: $zipcode\r\n Email Address: $email\r\n phone: $phone\r\n memory: $memory\r\n honor: $honor\r\n Checkbox: $checkbox"; 
	
	 $headers =    "MIME-Versin: 1.0\r\n" . 
               "Content-type: text/plain; charset=ISO-8859-1; format=flowed\r\n" . 
               "Content-Transfer-Encoding: 8bit\r\n" . 
               "From: $myemail\r\n" . 
                "Reply: $email_address\r\n".
               "Return-Path: $email_address\r\n".
               "X-Mailer: PHP" . phpversion();  


	
	mail($to,$email_subject,$email_body,$headers);
	//redirect to the 'thank you' page
header('Location: form_thanks.html');
exit();
} 
?>

<!doctype html>
<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>

The eregi function was removed in PHP 7.
Use preg_match instead of it. Though in this case you should use filter_var to validate an email address.
I guess the host updated from PHP 5.X

if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
   $errors .= "\r\n Error: Invalid email address";
}

I changed the (!eregi( to what you suggested. I found a website that is a PHP sandbox and ran the code. Here are the results.

Warning: Undefined array key "name" in /home/user/scripts/code.php on line 10

Warning: Undefined array key "address" in /home/user/scripts/code.php on line 11

Warning: Undefined array key "city" in /home/user/scripts/code.php on line 12

Warning: Undefined array key "state" in /home/user/scripts/code.php on line 13

Warning: Undefined array key "zipcode" in /home/user/scripts/code.php on line 14

Warning: Undefined array key "email" in /home/user/scripts/code.php on line 15

Warning: Undefined array key "phone" in /home/user/scripts/code.php on line 16

Warning: Undefined array key "memory" in /home/user/scripts/code.php on line 17

Warning: Undefined array key "honor" in /home/user/scripts/code.php on line 18

Warning: Undefined array key "checkbox" in /home/user/scripts/code.php on line 19

<!doctype html>
<html>
<head>
	<title>Contact form handler</title>
</head>

<body>
<!-- This page is displayed only if there is some error -->
<br />
 Error: all fields are required<br />
 Error: Invalid email address

</body>
</html>

I guess that is because there is no $_POST data from a form submission in your sandbox.
Really the whole form processing should be within a condition like this:-

if($_SERVER['REQUEST_METHOD'] == 'POST'){
    // Process form data here!!
}

So it only runs when the form is submitted.
But I guess that won’t work in your sandbox, as no post data will no longer result in an error, it will just mean nothing happens, because it no longer tries to process data that isn’t there.

I uploaded the PHP code to my test site and it works. At least I am not getting an error anymore. I asked my client to check her emails to make sure she got a test one from me.

I changed the exact same code on my contact form and I am still getting an error with mine. It says
Error: all fields are required
Error: Invalid email address

The only difference between her code and mine is at the top

<?php 
$errors = '';
$myemail = 'corifoxworthy@foxdenwebsolutions.com';
if(empty($_POST['name'])  || 
   empty($_POST['email']) || 
   empty($_POST['message']))
{
    $errors .= "\r\n Error: all fields are required";
}

The form on my client’s site now takes me to the thank you page after filling out the form but no email is sent with the info from the form. I changed the email address to mine to test it and did check my junk folder but no email so not sure that is causing this. Can someone please look at the code again and see what else might need to be updated in it?

The problem could be with your host and the PHP mail() function. You would be better off using something like PHPMailer to actually send the email. Apart form being more reliable, you can get diagnostics if needed.

I downloaded the PHPMailer zip file. I read through the readme file and have looked up several videos on how to use it. I have to admit that I am getting much older and am completely lost on what I need to do to get this all to work. This is the only site I still maintain as I have retired from web development or designing. I don’t want to have to rebuild the form I have on her site. I just want her to be able to get an email stating that someone has filled out the form in memory or honor of someone.

In my testing folder for her site, I have the contact-form-handler.php file and something called webformailer.php. Not sure what that is. The donate page has the form on it. I uploaded the file for PHPMailer and called it PHPMailer.

I also checked the error logs and am getting PHP warnings for undefined array keys for all the sections of the form. The godaddy site is running PHP 8.1.

Please be patient with me and walk me through what I need to do to get an email sent to my client that includes all the fields that she needs so she knows who filled out the form and if it is in memory or honor of someone. Thanks.

Good news! I did some more research because it was mentioned that it could be the PHP mail() function. I found a post from the godaddy community about changing the MX record and that fixed it. Form not throwing errors and email comes through.

However, on my site after filling out the form it still throws the error of all fields are required even though all fiends are populated with the correct info. Not sure what is causing that error.

1 Like

Did some more research and found that on the form itself I had when it should have been lower case. Now it all works. Weird that both of these forms worked years ago just find but things changed and caused problems. Now I am able to get my client’s form and my form to work and send the email from the form submission.

I didn’t know about the deprecated function so thank you for help with that.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.