The best way to prevent spam is by using CAPTCHA. Also, please re-post the final code you're using so we can see what's causing the data to double.
The best way to prevent spam is by using CAPTCHA. Also, please re-post the final code you're using so we can see what's causing the data to double.
- Josh
PHP Code:<?php
$to = "josh@gscapedesign.com";
$subject = "Greenscape Customer Inquiry";
$message = "Customer name: " . $_POST['name'] . "\r\n" .
"Phone number: " . $_POST['phone'] . "\r\n" .
"Email: " . $_POST['email'] . "\r\n" ."\r\n" .
"Best way to contact: " . $_POST['contact'] . "\r\n" ."\r\n";
if(isset($_POST[serviceItem])) {
if(isset($_POST[serviceItem])) {
$message .= "Services: ".implode(", ", $_POST[serviceItem])."\r\n";
}
}
$message .= "Budget: " . $_POST['budget'] . "\r\n" .
$message .= "Anything Else: " . $_POST['other'] . "\r\n" .
$from = $_POST['email'];
$headers = "From: $from" . "\r\n";
$headers = "Bcc: kroge16@tigers.lsu.edu" . "\r\n" ;
mail($to,$subject,$message,$headers) ;
?>
And here is the html with the submit button remedy you suggested...
HTML Code:<form action="email_form.php" method="post" name="contact_gscape" id="contact_gscape" onsubmit="document.getElementById('submit_btn').disabled = 'disabled'"> <span id="sprytextfield1"> <label for="name"><span class="form_text">Name</span></label> <input type="text" name="name" id="name" /> <span class="textfieldRequiredMsg">A contact name is required.</span></span> <br /> <span id="sprytextfield2"> <label for="phone" class="form_text">Phone Number</label> <input type="text" name="phone" id="phone" /> <span class="textfieldRequiredMsg">A phone number is required.</span><span class="textfieldInvalidFormatMsg">Please enter a valid phone number.</span></span> <br /> <span id="sprytextfield3"> <label for="email" class="form_text">Email</label> <input type="text" name="email" id="email" /> <span class="textfieldRequiredMsg">An email address is required.</span><span class="textfieldInvalidFormatMsg">Please enter a valid email address.</span></span> </p> <span id="contact"> <label for="label">What is the best way to contact you?</label> <select name="contact" size="1" id="contact"> <option value="Phone">Phone</option> <option value="Email">Email</option> </select> <span class="selectRequiredMsg">Please select an item.</span></span> </p> <span class="form_text">What Greenscape services would you like to know more about?</span> <br /> <label> <input name="serviceItem[]" type="checkbox" id="landscape design and construction" value="landscape design and construction" /> <span class="form_text">Landscape Design & Construction</span></label> <br /> <label> <input name="serviceItem[]" type="checkbox" id="hardscape design and construction" value="hardscape design and construction" /> <span class="form_text">Hardscape Design & Construction</span></label> <br /> <label> <input name="serviceItem[]" type="checkbox" id="grounds maintenance" value="grounds maintenance" /> <span class="form_text">Grounds Maintenance</span></label> <br /> <label> <input name="serviceItem[]" type="checkbox" id="irrigation and drainage systems" value="irrigation and drainage systems" /> <span class="form_text">Irrigation & Drainage Systems</span></label> <br /> <label> <input name="serviceItem[]" type="checkbox" id="lighting" value="lighting" /> <span class="form_text">Lighting</span></label> </p> <span class="form_text">What is your budget?</span><span id="level1"> <label for="budget"></label> <select name="budget" id="budget"> <option value="$0 - $500">$0 - $500</option> <option value="$501-$1000">$501-$1000</option> <option value="$1001-$5000">$1001-$5000</option> <option value="$5000+">$5000+</option> </select> <span class="selectRequiredMsg">Please select an item.</span></span> </p> <span id="sprytextarea1"> <label for="other" class="form_text">Anything else?</label> <br /> <textarea name="other" id="other" cols="45" rows="2"></textarea></span><br /> <input name="Submit" type="submit" value="Email Us" id="submit_btn" /> </form>
Should I perhaps use this code instead of trying to build my own???
http://www.work-at-home-forum.com/web-design-8/free-php-contact-form-with-spam-protection-12714.html
I see on another forum that someone has done something with php that starts like this
Would putting some sort of modification that way be beneficial?PHP Code:$from = $_POST['from'];
if(IsSet($from)){
I really don't know what I am doing, so if that was out of left field, I apologize.
Ok, I took the $message tags off of "Budget" and "Anything Else" - and now I am getting just one message (YAY!)... however, it is not populating anything from the text area or 'other'PHP Code:<?php
$to = "josh@gscapedesign.com";
$subject = "Greenscape Customer Inquiry";
$message = "Customer name: " . $_POST['name'] . "\r\n" .
"Phone number: " . $_POST['phone'] . "\r\n" .
"Email: " . $_POST['email'] . "\r\n" ."\r\n" .
"Best way to contact: " . $_POST['contact'] . "\r\n" ."\r\n";
if(isset($_POST[serviceItem])) {
if(isset($_POST[serviceItem])) {
$message .= "Services: ".implode(", ", $_POST[serviceItem])."\r\n";
}
}
"Budget: " . $_POST['budget'] . "\r\n" .
"Anything Else: " . $_POST['other'] . "\r\n" .
$from = $_POST['email'];
$headers = "From: $from" . "\r\n";
$headers = "Bcc: kroge16@tigers.lsu.edu" . "\r\n" ;
mail($to,$subject,$message,$headers) ;
?>
Oh, I see why there were duplicate messages now. The first$messagevariable string was never terminated, so it was essentially a$messagewithin$messagecausing it to duplicate (and that's why your solution worked).
Try using this:
PHP Code:<?php
$to = "josh@gscapedesign.com";
$subject = "Greenscape Customer Inquiry";
$message = "Customer name: " . $_POST['name'] . "\r\n" .
"Phone number: " . $_POST['phone'] . "\r\n" .
"Email: " . $_POST['email'] . "\r\n" ."\r\n" .
"Best way to contact: " . $_POST['contact'] . "\r\n" ."\r\n";
if(isset($_POST[serviceItem])) {
if(isset($_POST[serviceItem])) {
$message .= "Services: ".implode(", ", $_POST[serviceItem])."\r\n";
}
}
$budget =
"Budget: " . $_POST['budget'] . "\r\n" .
"Anything Else: " . $_POST['other'] . "\r\n";
$from = $_POST['email'];
$headers = "From: $from" . "\r\n";
$headers = "Bcc: kroge16@tigers.lsu.edu" . "\r\n" ;
mail($to,$subject,$message.$budget,$headers) ;
?>
- Josh
Okay - tried that and neither the budget or other showed up - but i am doing some trails to see what I can do.
Done some trials - here is what I have:
And it displays as such:PHP Code:<?php
$to = "katiebugla@yahoo.com";
$subject = "Greenscape Customer Inquiry";
$message = "Customer name: " . $_POST['name'] . "\r\n" .
"Phone number: " . $_POST['phone'] . "\r\n" .
"Email: " . $_POST['email'] . "\r\n" ."\r\n" .
"Best way to contact: " . $_POST['contact'] . "\r\n" ."\r\n" .
"Budget: " . $_POST['budget'] . "\r\n" ;
if(isset($_POST[serviceItem])) {
if(isset($_POST[serviceItem])) {
$message .= "Services: ".implode(", ", $_POST[serviceItem])."\r\n";
}
}
$message .= "Anything Else: " . $_POST['other'] . "\r\n";
$from = $_POST['email'];
$headers = "From: $from" . "\r\n";
$headers = "Bcc: kroge16@tigers.lsu.edu" . "\r\n" ;
mail($to,$subject,$message,$headers) ;
?>
Customer name: John Doe
Phone number: 1-xxx-xxx-xxxx
Email: blah@yahoo.com
Best way to contact: Email
Budget: $1001-$5000
Services: hardscape design and construction, grounds maintenance, lighting
Anything Else: For the love of meatballs, work.
Thank you for your patience - would you mind directing me to some info on making this more secure (i.e. less spam)?
Name this file captcha.php:
Insert this into your form HTML:PHP Code:<?php.
session_start();
$str = "";
$length = 0;
for ($i = 0; $i < 6; $i++) {
// these numbers represent ASCII table (small letters)
$str .= chr(rand(97, 122));
}
//md5 letters and saving them to session
$letters = md5($str);
$_SESSION['letters'] = $letters;
//determine width and height for our image and create it
$imgW = 300;
$imgH = 100;
$image = imagecreatetruecolor($imgW, $imgH);
//setup background color and border color
$backgr_col = imagecolorallocate($image, 238,239,239);
$border_col = imagecolorallocate($image, 208,208,208);
//let's choose color in range of purple color
$text_col = imagecolorallocate($image, rand(70,90),rand(50,70),rand(120,140));
//now fill rectangle and draw border
imagefilledrectangle($image, 0, 0, $imgW, $imgH, $backgr_col);
imagerectangle($image, 0, 0, $imgW-1, $imgH-1, $border_col);
//save fonts in same folder where you PHP captcha script is
//name these fonts by numbers from 1 to 3
//we shall choose different font each time
$fn = rand(1,3);
$font = $fn . ".ttf";
//setup captcha letter size and angle of captcha letters
$font_size = $imgH / 2.2;
$angle = rand(-15,15);
$box = imagettfbbox($font_size, $angle, $font, $str);
$x = (int)($imgW - $box[4]) / 2;
$y = (int)($imgH - $box[5]) / 2;
imagettftext($image, $font_size, $angle, $x, $y, $text_col, $font, $str);
//now we should output captcha image
header("Content-type: image/png");
imagepng($image);
imagedestroy ($image);
?>
And modify your form's PHP to this:Code:<p>Insert letters from image below:</p> <img src="http://yourdomain.com/captcha.php" width="300" height="100" alt="simple PHP captcha" /> <input type="text" name="captcha" />
That will add a CAPTCHA image, where users have to enter the letters they see into the textbox. Robots can't do this, so it prevents spam.PHP Code:<?php
session_start();
if (!$is_valid) {
echo 'Letters from simple PHP captcha have not been entered correctly....';
} else {
$to = "josh@gscapedesign.com";
$subject = "Greenscape Customer Inquiry";
$message = "Customer name: " . $_POST['name'] . "\r\n" .
"Phone number: " . $_POST['phone'] . "\r\n" .
"Email: " . $_POST['email'] . "\r\n" ."\r\n" .
"Best way to contact: " . $_POST['contact'] . "\r\n" ."\r\n";
if(isset($_POST[serviceItem])) {
$message .= "Services: ".implode(", ", $_POST[serviceItem])."\r\n";
}
$budget =
"Budget: " . $_POST['budget'] . "\r\n" .
"Anything Else: " . $_POST['other'] . "\r\n";
$from = $_POST['email'];
$headers = "From: $from" . "\r\n";
$headers = "Bcc: kroge16@tigers.lsu.edu" . "\r\n" ;
mail($to,$subject,$message.$budget,$headers) ;
$is_valid = $_SESSION['letters'] == md5(strtolower($_POST['captcha']));
echo 'CAPTCHA has been entered correctly.';
}
?>
Script credit:
http://stuntsnippets.com/simple-php-captcha/
- Josh
katiebugla (09-01-2011)
Bookmarks