Leaf Posted January 16, 2011 Share Posted January 16, 2011 Hi My PHP knowledge is very very small. This code was written for me by a friend but everytime I ask him for help I end up being more confused then I was anyways . It was working perfectly until I changed my webspace to another provider. <?php if ($email == "") { require_once ("xyz1.html"); } else { $pos = strrpos($email, "@"); if ($pos != false) { $to = "email@address.com"; $headers = "From: \"".$name."\"<".$email.">\n"; $message = " \n Name: $name , ..."; $sent = mail ($to, mailheadersubject, $message, $headers); require_once ("send.html"); } else { require_once ("xyz2.html"); } } ?> ... and the HTML part: <form action="send.php" method="GET"> Apart from that it's just a normal HTML contact form. As I said nothing has changed exepct the provider, so my only guess would be that the problem is that part of this code doesn't work as it used to on a newer version of PHP. What it did was - as you probably see in the code anyway - checking if there was something written in the contact form where you put in your email, else show xyz1.html , then checking if there is a "@" in the email address and if there is send the email, if not posting the xyz2.html. Now it always shows the xyz1.html, no matter what is written in the email part of the contact form. I hope there is a small simple solution to the problem, but I'm grateful for any help I can get greetings Quote Link to comment https://forums.phpfreaks.com/topic/224639-contact-form-problem/ Share on other sites More sharing options...
jcbones Posted January 16, 2011 Share Posted January 16, 2011 I made a *few* changes, most importantly, I commented the code for you to see what is happening. <?php $email = strip_tags($_GET['email']); $name = strip_tags($_GET['name']); if (empty($email)) { //if the email string is empty. require_once ("xyz1.html"); //require the server to load the xyz1.html file,or else spit out an error. } else { //if $_GET['email'] is NOT empty. $pos = strrpos($email, "@"); //find out if email string has @ in it. if ($pos != false) { //if the position of the @ symbol is not the first character, and it IS in the string. $to = "email@address.com"; //identify your email address explicitly. $headers = "From: \"".$name."\"<".$email.">\n"; //add their email address in the from field. NOTE* Some hosts require this field to be poplulated by an email address that resides on YOUR server (ie. your email address). $message = " \n Name: $name , ..."; //build message. if(mail ($to, mailheadersubject, $message, $headers)) { //send mail. require_once ("send.html"); //if mail successfully sent, include this file. } else { die('There was an error sending the message'); //if mail failed to send message, tell us. } } else { require_once ("xyz2.html"); //if the @ symbol was not found, include this file. } } Quote Link to comment https://forums.phpfreaks.com/topic/224639-contact-form-problem/#findComment-1160357 Share on other sites More sharing options...
Leaf Posted January 17, 2011 Author Share Posted January 17, 2011 Hi jcbones Very nice, thanks. Works perfectly. Only one question left on something I am not sure. The strip_tags thing at the start. Is this only for the $headers thing or should I add it for all the variables in the $message array (hope that's the correct term .. didn't want to use "thing" everytime ..) too? It works without them too but just to be on the save side. greetings Quote Link to comment https://forums.phpfreaks.com/topic/224639-contact-form-problem/#findComment-1160474 Share on other sites More sharing options...
jcbones Posted January 17, 2011 Share Posted January 17, 2011 It strips out any html tags someone would enter into the form. Is is a *little* sanitation of your form, more could be added. It is more important that line be on your "name" field than the "email" field, since you add the name field into your message. If headers are wrong, you won't get the message, I would make sure the message is clean IF/WHEN I get it. Quote Link to comment https://forums.phpfreaks.com/topic/224639-contact-form-problem/#findComment-1160494 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.