S_M_E Posted February 12, 2009 Share Posted February 12, 2009 I had a contact form made for one of my sites but I had issues with PHP4 vs PHP5 which are now resolved but there are a couple of basic fixes that I need to make and the person that made the form is no longer reachable. 1) Name, email and comments fields should all be required. 2) Name and email are not being passed from the contact form to the email (only the comments are currently passed) when submitted and name, email and comments should be on separate lines preferably. Here's the complete form: <?php $email_sent = 0; if(isset($_POST["contact"])){ require("./config.php"); if(mail(MainConfig::$mail, "Contact Us","$_POST[comments]")) $email_sent = 1; } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>IAMAPATIENT</title> <link rel="stylesheet" type="text/css" href="css/styles.css" /> </head> <body> <div id="Wrapper"> <div id="Header"> <img src="images/logo.gif" width="100px" height="100px" > <h1>IAMAPATIENT</h1> <img src="images/logos.gif" width="100px" height="100px" style="float:right; margin-right:20px;" > <div class="clear"></div> </div> <div id="Content"> <div class="hook-1"> <div id="Left"> <ul> <li><a href="index.html">Home</a></li> <li><a href="whoweare.php">Who We Are</a></li> <li><a href="donor.php">Donor</a></li> <li><a href="contactus.php">Contact Us</a></li> <li><a href="links.html">Links</a></li> </ul> </div> <div id="Right"> <div id="ImgBar"> <img src="images/img2.gif" /> <img src="images/img3.gif" /> <img src="images/img1.gif" /> <img src="images/img4.gif" /> <div class="clear"></div> </div> <div id="PageContent"> <h2>Request More Information</h2> <p> Use this form to contact us to get more information or about our company, products, or services. Suggestions welcomed! </p> <?php if($email_sent) echo "<p>Thanks, We got your email</p>"; ?> <form action="#" method="post" > <table> <tr> <td>Name:</td><td><input type="text" name="name" /></td> </tr> <tr> <td>Email:</td><td><input type="text" name="email" /></td> </tr> <tr> <td valign="top">Comments:</td><td><textarea name="comments" cols="30" rows="10"></textarea></td> </tr> <tr> <td></td><td><input type="submit" value="Submit" name="contact" /></td> </tr> </table> </form> </div> </div> <div class="clear"></div> </div> </div> </div> </body> </html> I see where to add the name and email but I don't know the proper format: if(mail(MainConfig::$mail, "Contact Us","$_POST[comments]")) Is it [name],,[comments], [name,email,comments]; should I use semicolons or is it something different maybe? I'm completely lost. I have no idea where or how to make the fields required or how to tell the visitor what fields are required if they don't enter one. Please advise. TIA... Quote Link to comment Share on other sites More sharing options...
drisate Posted February 12, 2009 Share Posted February 12, 2009 Proper format is <?php $Name = "Da Duder"; //senders name $email = "email@adress.com"; //senders e-mail adress $recipient = "PersonWhoGetsIt@emailadress.com"; //recipient $mail_body = "The text for the mail..."; //mail body $subject = "Subject for reviever"; //subject $header = "From: ". $Name . " <" . $email . ">\r\n"; //optional headerfields mail($recipient, $subject, $mail_body, $header); //mail command ?> Quote Link to comment Share on other sites More sharing options...
drisate Posted February 12, 2009 Share Posted February 12, 2009 Hmm i think the best thing would be to create a function for the form like this: <?php if (!$_POST[name]) { $ERROR = $ERROR . "No name!"; } if (!$_POST[email]) { $ERROR = $ERROR . "No email!"; } if (!$_POST[comments]) { $ERROR = $ERROR . "No comments!"; } $email_sent = 0; if (!$ERROR) { $recipient = "PersonWhoGetsIt@emailadress.com"; //recipient $subject = "Comment Systeme"; //subject $header = "From: " . $_POST[name] . " <" . $_POST[email] . ">\r\n"; //optional headerfields mail($recipient, $subject, $_POST[comments], $header); //mail command $ERROR = "SUCCES!"; } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>IAMAPATIENT</title> <link rel="stylesheet" type="text/css" href="css/styles.css" /> </head> <body> <div id="Wrapper"> <div id="Header"> <img src="images/logo.gif" width="100px" height="100px" > <h1>IAMAPATIENT</h1> <img src="images/logos.gif" width="100px" height="100px" style="float:right; margin-right:20px;" > <div class="clear"></div> </div> <div id="Content"> <div class="hook-1"> <div id="Left"> <ul> <li><a href="index.html">Home</a></li> <li><a href="whoweare.php">Who We Are</a></li> <li><a href="donor.php">Donor</a></li> <li><a href="contactus.php">Contact Us</a></li> <li><a href="links.html">Links</a></li> </ul> </div> <div id="Right"> <div id="ImgBar"> <img src="images/img2.gif" /> <img src="images/img3.gif" /> <img src="images/img1.gif" /> <img src="images/img4.gif" /> <div class="clear"></div> </div> <div id="PageContent"> <h2>Request More Information</h2> <p> Use this form to contact us to get more information or about our company, products, or services. Suggestions welcomed! </p> <?php form($ERROR); ?> </div> </div> <div class="clear"></div> </div> </div> </div> </body> </html> <?php function form($ERROR) { if ($ERROR) { echo "<b>$ERROR</b><br>"; } print ('<form action="#" method="post"> <table> <tr> <td>Name:</td> <td><input type="text" name="name" /></td> </tr> <tr> <td>Email:</td> <td><input type="text" name="email" /></td> </tr> <tr> <td valign="top">Comments:</td> <td><textarea name="comments" cols="30" rows="10"></textarea></td> </tr> <tr> <td></td> <td><input type="submit" value="Submit" name="contact" /></td> </tr> </table> </form>'); } ?> Quote Link to comment Share on other sites More sharing options...
S_M_E Posted February 12, 2009 Author Share Posted February 12, 2009 I'm sorry, I should have mentioned that it also uses a config.php file to set the email recipient. <?php // SET THE CONTACT US EMAIL IN THIS LINE MainConfig::set_email("recipient@some-email.com"); class MainConfig{ static $mail = ""; static function set_email($email){ self::$mail = $email; } } ?> Quote Link to comment Share on other sites More sharing options...
drisate Posted February 12, 2009 Share Posted February 12, 2009 well no need of a class for that ;-) Give the var the value and thats it hehe Quote Link to comment Share on other sites More sharing options...
S_M_E Posted February 12, 2009 Author Share Posted February 12, 2009 I think the reason for that was to prevent a visitor (or a bot) from looking at the source of contactus.php and harvesting the email address. Quote Link to comment Share on other sites More sharing options...
drisate Posted February 12, 2009 Share Posted February 12, 2009 Well the var is in PHP after the form was posted so he can't :-) It's imposible to look at the source of a PHP page $recipient = "PersonWhoGetsIt@emailadress.com"; //recipient $subject = "Comment Systeme"; //subject $header = "From: " . $_POST[name] . " <" . $_POST . ">\r\n"; //optional headerfields mail($recipient, $subject, $_POST[comments], $header); //mail command $ERROR = "SUCCES!"; The only bot you should care abbout are the spamer bot's lol all you have to do is add a captcha and they can't submit anything :-) Quote Link to comment Share on other sites More sharing options...
S_M_E Posted February 12, 2009 Author Share Posted February 12, 2009 Well the var is in PHP after the form was posted so he can't :-) It's imposible to look at the source of a PHP page $recipient = "PersonWhoGetsIt@emailadress.com"; //recipient $subject = "Comment Systeme"; //subject $header = "From: " . $_POST[name] . " <" . $_POST . ">\r\n"; //optional headerfields mail($recipient, $subject, $_POST[comments], $header); //mail command $ERROR = "SUCCES!"; The only bot you should care abbout are the spamer bot's lol all you have to do is add a captcha and they can't submit anything :-) Right, you are. I forgot that they can't see the functions, only the html parts. I would add captcha but it would have to be a less obfuscated one. The better ones might drive off people that can't figure them out. How could I add a simple captcha to the form? For example to add: "please type the word "V E T E R A N S" (without the spaces) here" type captcha? Quote Link to comment Share on other sites More sharing options...
drisate Posted February 12, 2009 Share Posted February 12, 2009 Would look like this :-) <?php /* The following code use to create random numbers and this number are embedding with existing image file, the first line used to initiate session, which use to carry the user inputs. */ session_start(); $RandomStr = md5(microtime()); $ResultStr = substr($RandomStr,0,5); $NewImage =imagecreatefromjpeg("img.jpg"); /* The second line [md5 (microtime ())] use to generate the random string, and the resultant string is trim by using third line [substr], which returns the portion of string specified by the start and length parameters. The function imagecreatefromjpeg ("img.jpg") is use to create a image by existing image file and as back ground ,so that you need to give an image file path. */ $LineColor = imagecolorallocate($NewImage,233,239,239); $TextColor = imagecolorallocate($NewImage, 255, 255, 255); imageline($NewImage,1,1,40,40,$LineColor); imageline($NewImage,1,100,60,0,$LineColor); imagestring($NewImage, 5, 20, 10, $ResultStr, $TextColor); /* After creation of back ground image, we generate some linear line, which is use to avoid the phrasing from random numbers, the respective lines are create by the function named imageline () and imagestring () use to draw a random string horizontally. */ $_SESSION['key'] = $ResultStr; /* The resultant random number [trimmed one], carry through session especially for validation purpose. */ header("Content-type: image/jpeg"); imagejpeg($NewImage); /* Finally above two functions are uses to display/out put the image to browser. So we can just call the particular file by through image source path, it will display the final image. */ if (!$_POST[name]) { $ERROR = $ERROR . "No name!"; } if (!$_POST[email]) { $ERROR = $ERROR . "No email!"; } if (!$_POST[comments]) { $ERROR = $ERROR . "No comments!"; } if(isset($_REQUEST['Submit'])){ $key=substr($_SESSION['key'],0,5); $number = $_REQUEST['number']; if($number!=$key){ $ERROR = $ERROR . "Validation string not valid! Please try again!"; }else{ if (!$ERROR) { $recipient = "PersonWhoGetsIt@emailadress.com"; //recipient $subject = "Comment Systeme"; //subject $header = "From: " . $_POST[name] . " <" . $_POST[email] . ">\r\n"; //optional headerfields mail($recipient, $subject, $_POST[comments], $header); //mail command $ERROR = "SUCCES!"; } } ?> You can download the captcha from this adress http://www.codewalkers.com/codefiles/630_PHP-CAPTCHA.zip Quote Link to comment Share on other sites More sharing options...
S_M_E Posted February 12, 2009 Author Share Posted February 12, 2009 That's a little more complicated than the type I was thinking about but it is an option. I'll have to think about it for a bit, I don't want real visitors to leave because they can't figure out the captcha. Thanks for the input, I appreciate it... Quote Link to comment 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.