Jump to content

PHP CONTACT FORM STOPPED WORKING - Help!


bewdleygirl
Go to solution Solved by Barand,

Recommended Posts

Hi,

Just wondered if anyone can help? A contact form which has previously worked has stopped working, I presume because the PHP has been upgraded to version 5.6.

If anyone is able to advise on how I need to tweak it, it would be massively appreciated! Code below:

Thanks, Sarah smile.gif

<?

// edit these lines

$your_name="Company Name";

$your_email="sarah@companyname.co.uk";

$your_web_site_name="companyname.co.uk";





?>



               <?php

               //If the form is submitted

               if(isset($_POST['name'])) {

                  

                     //Check to make sure that the name field is not empty

                     if(trim($_POST['name']) === '') {

                        $nameError = 'Please enter your name.';

                        $hasError = true;

                     } else {

                        $name = trim($_POST['name']);

                     }

                     

                     //Check to make sure sure that a valid email address is submitted

                     if(trim($_POST['email']) === '')  {

                        $emailError = 'Please enter your email address.';

                        $hasError = true;

                     } else if (!eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($_POST['email']))) {

                        $emailError = 'You entered an invalid email address.';

                        $hasError = true;

                     } else {

                        $email = trim($_POST['email']);

                     }

                        

                     //Check to make sure comments were entered   

                     if(trim($_POST['message']) === '') {

                        $commentError = 'Please enter your message.';

                        $hasError = true;

                     } else {

                        if(function_exists('stripslashes')) {

                           $comments = stripslashes(trim($_POST['message']));

                        } else {

                           $comments = trim($_POST['message']);

                        }

                     }

                        

                     //If there is no error, send the email

                     if(!isset($hasError)) {



                        $emailTo = $your_email;

                        $subject = 'Contact Form Submission from '.$name;

                        $body = "Name: $name \n\nEmail: $email \n\nPhone: ".trim($_POST['phone'])." \n\nComments: $comments";

                        $headers = 'From: '.$your_web_site_name.' <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;

                        

                        mail($emailTo, $subject, $body, $headers);



                        $emailSent = true;



                  }

               }

               

               ?>







<?php if(isset($emailSent) == true) { ?>

   <div class="ok">

      <h1>Thanks, <?php echo $name;?></h1>

      <p>Your email was successfully sent. We will be in touch soon.</p>

   </div>

<?php } ?>





<?php if(isset($hasError) ) { ?>

   <div class="error2">There was an error submitting the form.</div>

<?php } ?>
Link to comment
Share on other sites

Also, eregi() should not be used - it is deprecated. It can still work, but based on the error level set for the server it may be stopping execution due to a warning about the function being deprecated. But, since you provided no details about any errors/warnings . . .

Link to comment
Share on other sites

Thanks a lot for the replies!

 

Sorry should have explained, there's no error when the form is used, you get the normal message 'Thanks for contacting us etc' but the email is not being generated and received. So the end user thinks their enquiry has succesfully been sent but it's not coming through.

 

I'll try changing <? thanks and see if that could be the issue.

Link to comment
Share on other sites

Thanks Psycho :)

 

I've dug out the line in question below. Should I replace eregi with something else then?

 

} else if (!eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($_POST['email']))) {

 

I'm really sorry but I work on frontend websites so have a very, very basic understanding of PHP. Any pointers would be much appreciated.

Link to comment
Share on other sites

Your code assumes the email gets sent, even without checking to see if it does.

 

Change these 2 lines:

mail($emailTo, $subject, $body, $headers);
$emailSent = true; //shouldn't assume this

to 

$emailSent = mail($emailTo, $subject, $body, $headers);
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.