|
![]() |
#1 |
![]() ![]() Join Date: Aug 2005
Location: Bali
Posts: 11,216
|
![]() here is a simple form that will send a mail to you from a page on your web site.
First a form using some html5 validation that will show errors when you submit. I have made some fields required, email and url will be validated the action is set to send.php where the mail sending script will reside. Code:
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Contact</title> <style> #form{ position:relative; width: 500px; margin:0 auto; } label { float:left; clear:left; } input[type="text"], [type="url"], [type="email"]{ float:left; color: #555 ; width: 70%; border: 1px solid #e5e5e5; background: #fbfbfb; height: 25px; } .textarea{ float:left; height:100px; width: 70%; } </style> </head> <body> <div id="form"> <form action="send.php" method="post"> <fieldset> <label for="name">Name:</label> <input id="name" name="name" type="text" required placeholder=" Your name" > </fieldset> <fieldset> <label >Email:</label> <input type="email" id="email" name="email" placeholder="me@mymail.com" required > </fieldset> <fieldset> <label>Website:</label> <input type="url" id="website" name="website" placeholder=" http://mysite.com" required > </fieldset> <fieldset> <label>Message</label> <textarea class="textarea" name="Message" id="Message" ></textarea> </fieldset> <fieldset> <input type="submit" name="Submit" value="Submit"> <input name="reset" type="reset"> </fieldset> </form> </div> </body> </html> there is a var named $th_url that is the url redirect that the visitor will be directed to after a successful submission, with no errors. send.php Code:
<?php if (isset($_POST['Submit']) && $_POST['name'] != "") { $th_url= "thankyou.html"; // modify to page redirect after mail succsessfully sent if ($_POST['name'] != "") { $_POST['name'] = filter_var($_POST['name'], FILTER_SANITIZE_STRING); if ($_POST['name'] == "") { $errors .= 'Please enter a valid name.<br/><br/>'; } } else { $errors .= 'Please enter your name.<br/>'; } if ($_POST['email'] != "") { $email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL); if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { $errors .= "$email is <strong>NOT</strong> a valid email address.<br/><br/>"; } } else { $errors .= 'Please enter your email address.<br/>'; } /* Validating web address in form field ********************************************** */ if ($_POST['website'] != "") { $website = filter_var($_POST['website'], FILTER_SANITIZE_URL); if (!filter_var($website, FILTER_VALIDATE_URL)) { $errors .= "$website is <strong>NOT</strong> a valid URL.<br/><br/>"; } } else { $errors .= 'Please enter your home page.<br/>'; } /* end Validating web address in form field ********************************************** */ if ($_POST['Message'] != "") { $_POST['Message'] = filter_var($_POST['Message'], FILTER_SANITIZE_STRING); if ($_POST['Message'] == "") { $errors .= 'Please enter a message to send.<br/>'; } } else { $errors .= 'Please enter a message to send.<br/>'; } if (!$errors) { $to = 'me@somewhere.com'; $subject = 'New Mail from ' . $_POST['email']; $message = 'From: ' . $_POST['name'] . "\n"; $message .= 'Email from: ' . $_POST['email'] . "\n"; $message .= "Message:\n" . $_POST['Message'] . "\n\n"; mail($to, $subject, $message); header("Location: $th_url"); } else { echo '<div style="color: red">' . $errors . '<br/></div>'; } } ?> i intend to extend this with some more examples as anti spam , time permitting
__________________
If you're happy and you know it shake your meds! different style links examples Flight / Hotel search Free script download Bali Villas Last edited by edbr; 05-11-2015 at 07:41 AM.. |
![]() |
![]() |
![]() |
#2 |
![]() Join Date: Feb 2006
Location: finland
Posts: 1,684
|
![]() Ed - just trying this out but feel i'm missing something really obvious...
tried it locally in mamp and it allegedly sent, got to the thank you page but it simply doesn't arrive at my email. uploaded it to http://gekkoweb.com/hl2015/form.html and still no joy. i removed the web site field from the form and the send.php i used my email address and plugged it in here: if (!$errors) { $mail_to = 'gekkoweb@gmail.com'; $subject = 'New Mail from ' . $_POST['email']; $message = 'From: ' . $_POST['name'] . "\n"; $message .= 'Email from: ' . $_POST['email'] . "\n"; $message .= "Message:\n" . $_POST['Message'] . "\n\n"; mail($to, $subject, $message); I suspect that this is where i'm going wrong "the action is set to send.php where the mail sending script will reside" - so my cgi bin or ??? this is really useful because for a while now i've been looking for a simple form like this but there's so much crud on the net finding something decent and made in the last 10 years ![]() ![]() |
![]() |
![]() |
![]() |
#3 |
![]() ![]() Join Date: Aug 2005
Location: Bali
Posts: 11,216
|
![]() id better test it, ime prone to rushinhg
![]() As for path the setup is for the form p[age and mailsending form to be in the same directory, another opyion is toset the action to itself action=" " and paste the php code at the page starht before the html
__________________
If you're happy and you know it shake your meds! different style links examples Flight / Hotel search Free script download Bali Villas |
![]() |
![]() |
![]() |
#4 |
![]() ![]() Join Date: Aug 2005
Location: Bali
Posts: 11,216
|
![]() yup its me
$mail_to = 'me@somewhere.com'; should read $to = 'me@somewhere.com';
__________________
If you're happy and you know it shake your meds! different style links examples Flight / Hotel search Free script download Bali Villas |
![]() |
![]() |
![]() |
#5 |
![]() ![]() Join Date: Aug 2005
Location: Bali
Posts: 11,216
|
![]() I just edited it and tested live and all is ok. Sorry about that
![]() ![]()
__________________
If you're happy and you know it shake your meds! different style links examples Flight / Hotel search Free script download Bali Villas |
![]() |
![]() |
![]() |
#6 |
![]() Join Date: Feb 2006
Location: finland
Posts: 1,684
|
![]() yep all works now, thanks.
embarassed because i've been doing this a long time and i should know how to do it by now, though i've never learnt php... one more quick qestion for my benefit and anyone else using this thread: where you have // modify to page redirect after mail succsessfully sent after taking the user to the thankyou page, could you detail the redirect bit for us? or is it a better to just provide the user with a normal link back to the homepage? look froward to the antispam ![]() |
![]() |
![]() |
![]() |
#7 |
![]() ![]() Join Date: Aug 2005
Location: Bali
Posts: 11,216
|
![]() i would say that depends. f'rinstance, on a job site i redirect back to the new jobs on others a thank you page with various link options, or sometimes the index page
__________________
If you're happy and you know it shake your meds! different style links examples Flight / Hotel search Free script download Bali Villas |
![]() |
![]() |
![]() |
#8 |
![]() ![]() Join Date: Aug 2005
Location: Bali
Posts: 11,216
|
![]() i will post a version with html and an anti spam 'honey trap' tomorrow
__________________
If you're happy and you know it shake your meds! different style links examples Flight / Hotel search Free script download Bali Villas |
![]() |
![]() |
![]() |
#9 |
![]() ![]() Join Date: Aug 2005
Location: Bali
Posts: 11,216
|
![]() ok this adds a couple of things to include a honey-pot or honey trap.
in essence, it is a form that should not get filled in. if your site is being spammed by a bot it will with luck fill in the field. the receiving php script will not allow the mail then to be sent. it is pretty effective , although not foolproof but then i have not found any way that is 100% effective, even captchas which can be used as well as a honey trap. ok the form adds a field, in this case i have called it myrealname . I have also added a css class that will not allow the input to be shown. This can be removed or commented out while testing if you want .robotic { display: none; } so the code Code:
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Contact</title> <style> #form{ position:relative; width: 500px; margin:0 auto; } label { float:left; clear:left; } input[type="text"], [type="url"], [type="email"]{ float:left; color: #555 ; width: 70%; border: 1px solid #e5e5e5; background: #fbfbfb; height: 25px; } .textarea{ float:left; height:100px; width: 70%; } .robotic { display: none; } </style> </head> <body> <div id="form"> <form action="" method="post"> <fieldset> <label for="name">Name:</label> <input id="name" name="name" type="text" required placeholder=" Your name" > </fieldset> <fieldset> <label >Email:</label> <input type="email" id="email" name="email" placeholder="me@mymail.com" required > </fieldset> <fieldset> <label>Website:</label> <input type="url" id="website" name="website" placeholder=" http://mysite.com" required > </fieldset> <fieldset> <label>Message</label> <textarea class="textarea" name="Message" id="Message" ></textarea> </fieldset> <fieldset> <input class="robotic" type="text" name="myrealname" id="myrealname"> <input type="submit" name="Submit" value="Submit"> <input name="reset" type="reset"> </fieldset> </form> </div> </body> </html> Code:
<?php $machine = $_POST['myrealname']; if ($machine != "") { echo 'naff off spammer'; exit(); //if a spambot filled out the "machine" //field, we don't proceed } else{ if (isset($_POST['Submit']) && $_POST['name'] != "") { $th_url= "thankyou.html"; // modify to page redirect after mail succsessfully sent if ($_POST['name'] != "") { $_POST['name'] = filter_var($_POST['name'], FILTER_SANITIZE_STRING); if ($_POST['name'] == "") { $errors .= 'Please enter a valid name.<br/><br/>'; } } else { $errors .= 'Please enter your name.<br/>'; } if ($_POST['email'] != "") { $email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL); if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { $errors .= "$email is <strong>NOT</strong> a valid email address.<br/><br/>"; } } else { $errors .= 'Please enter your email address.<br/>'; } /* Validating web address in form field ********************************************** */ if ($_POST['website'] != "") { $website = filter_var($_POST['website'], FILTER_SANITIZE_URL); if (!filter_var($website, FILTER_VALIDATE_URL)) { $errors .= "$website is <strong>NOT</strong> a valid URL.<br/><br/>"; } } else { $errors .= 'Please enter your home page.<br/>'; } /* end Validating web address in form field ********************************************** */ if ($_POST['Message'] != "") { $_POST['Message'] = filter_var($_POST['Message'], FILTER_SANITIZE_STRING); if ($_POST['Message'] == "") { $errors .= 'Please enter a message to send.<br/>'; } } else { $errors .= 'Please enter a message to send.<br/>'; } if (!$errors) { $to = 'me@gmail.com'; $subject = 'New Mail from ' . $_POST['email']; $message = 'From: ' . $_POST['name'] . "\n"; $message .= 'Email from: ' . $_POST['email'] . "\n"; $message .= "Message:\n" . $_POST['Message'] . "\n\n"; mail($to, $subject, $message); header("Location: $th_url"); } else { echo '<div style="color: red">' . $errors . '<br/></div>'; } } } ?>
__________________
If you're happy and you know it shake your meds! different style links examples Flight / Hotel search Free script download Bali Villas Last edited by edbr; 05-13-2015 at 01:09 AM.. |
![]() |
![]() |
![]() |
#10 |
![]() Join Date: Feb 2006
Location: finland
Posts: 1,684
|
![]() that's great ed, thanks loads. will have a look at that later today
![]() |
![]() |
![]() |
![]() |
Thread Tools | |
Display Modes | |
|
|