bewdleygirl Posted January 7, 2015 Share Posted January 7, 2015 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 <? // 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 } ?> Quote Link to comment https://forums.phpfreaks.com/topic/293727-php-contact-form-stopped-working-help/ Share on other sites More sharing options...
CroNiX Posted January 7, 2015 Share Posted January 7, 2015 Is there an error on the screen, or in the error log? Define "stopped working" Quote Link to comment https://forums.phpfreaks.com/topic/293727-php-contact-form-stopped-working-help/#findComment-1502017 Share on other sites More sharing options...
Solution Barand Posted January 7, 2015 Solution Share Posted January 7, 2015 One problem could be the use of <? instead of <?php on line 1 Quote Link to comment https://forums.phpfreaks.com/topic/293727-php-contact-form-stopped-working-help/#findComment-1502019 Share on other sites More sharing options...
Psycho Posted January 7, 2015 Share Posted January 7, 2015 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 . . . Quote Link to comment https://forums.phpfreaks.com/topic/293727-php-contact-form-stopped-working-help/#findComment-1502020 Share on other sites More sharing options...
bewdleygirl Posted January 7, 2015 Author Share Posted January 7, 2015 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. Quote Link to comment https://forums.phpfreaks.com/topic/293727-php-contact-form-stopped-working-help/#findComment-1502021 Share on other sites More sharing options...
bewdleygirl Posted January 7, 2015 Author Share Posted January 7, 2015 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. Quote Link to comment https://forums.phpfreaks.com/topic/293727-php-contact-form-stopped-working-help/#findComment-1502023 Share on other sites More sharing options...
hansford Posted January 7, 2015 Share Posted January 7, 2015 use preg_match() Quote Link to comment https://forums.phpfreaks.com/topic/293727-php-contact-form-stopped-working-help/#findComment-1502024 Share on other sites More sharing options...
bewdleygirl Posted January 7, 2015 Author Share Posted January 7, 2015 Thanks so much Barand, it's solved the problem! Quote Link to comment https://forums.phpfreaks.com/topic/293727-php-contact-form-stopped-working-help/#findComment-1502034 Share on other sites More sharing options...
CroNiX Posted January 7, 2015 Share Posted January 7, 2015 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); Quote Link to comment https://forums.phpfreaks.com/topic/293727-php-contact-form-stopped-working-help/#findComment-1502060 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.