You don't need MySQL for this. :-) and I'd like to officially say I hate javascript with a passion and I never want to touch regular expressions again unless I have to. :-)
Rather small and easy to understand script...
| CODE |
<script language="JavaScript" type="text/javascript"> function check(form){ if (form.name.value == "") { alert("Please enter your name."); form.name.focus(); return false; } else if (form.email.value == "") { alert("Please enter your email."); form.email.focus(); return false; } else if(!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(form.email.value))){ alert("Please enter a valid email address."); form.email.focus(); return false; } else if (form.comment.value == "") { alert("Please enter a comment/question."); form.comment.focus(); return false; } else{ return true; } } </script> |
| CODE |
<?php function mailcheck($emailadr){ return eregi("^[a-zA-Z0-9_\.-]+@[a-zA-Z0-9\.-]+\.[a-zA-Z]{2,4}$", $emailadr); } if (!isset($_POST['submit'])) #if submit doesn't have a value { ?> <h3>Feel free to contact me.</h3> <form method="post" name="emailform" action="<?php echo $_SERVER['SCRIPT_NAME']?>" onSubmit="return check(this);"> <p>Name : <input type="text" name="name" /></p> <p>Email : <input type="text" name="email" /></p><br /> <p>Comments/Questions : <br /><textarea name="comment" rows="10" cols="30"></textarea></p> <p><input type="submit" name="submit" value="Submit" /></p> </form> <?php } else { $name = $_POST['name']; $email = $_POST['email']; $comment = stripslashes($_POST['comment']); $to = "Your Name<YourEmail@YourDomain.com> "; if ((mailcheck($_POST['email']))||($comment!=NULL)||($name!=NULL)) #check if a real email address, if it returns true it sends the email #also checks to make sure the comment and name fields aren't empty { # subject $subject = "Mail from site."; # message $message = ' <html> <head> <title>Mail from site</title> </head> <body> <p>The email contains user comments.</p> <br /> Name of person : '.$name.'<br /> #I have it displaying the name Email of person : '.$email.'<br />#here the email Comment/Question left:<br /> <p>'.$comment.'</p>#here the comment <br /> #it uses html so feel free to format to your pleasure </body> </html> '; $headers = "MIME-Version: 1.0\r\n"; #headers $headers .= "Content-type: text/html; charset=iso-8859-1\r\n"; $headers .= "From: $email>\r\n"; #The sender's email #Will show up in your box from their email address #mail it mail($to, $subject, $message, $headers); echo 'Thank you for sending mail.'; } else { echo 'Sorry, one of more or your fields is invalid.';} } ?> |
| CODE |
if (form.subject.value == "") { alert("Please enter a subject."); form.name.focus(); return false; |
| CODE |
<p>Email : <input type="text" name="subject" /></p><br /> |
| CODE |
<?php } else { $name = $_POST['name']; $email = $_POST['email']; $subject = $_POST['subject']; $comment = $_POST['comment']; $to = "Your Name<YourEmail@YourDomain.com> "; if ((mailcheck($_POST['email']))||($comment!=NULL)||($name!=NULL)||($subject!=NULL)) #check if a real email address, if it returns true it sends the email #also checks to make sure the comment and name fields aren't empty { # subject #$subject = "mail from site" deleted # message $message = ' <html> <head> <title>$subject</title> </head> <body> <p>The email contains user comments.</p> <br /> Name of person : '.$subject.'<br /> Name of person : '.$name.'<br /> #I have it displaying the name Email of person : '.$email.'<br />#here the email Comment/Question left:<br /> <p>'.$comment.'</p>#here the comment <br /> #it uses html so feel free to format to your pleasure </body> </html> '; $headers = "MIME-Version: 1.0\r\n"; #headers $headers .= "Content-type: text/html; charset=iso-8859-1\r\n"; $headers .= "From: $email>\r\n"; #The sender's email #Will show up in your box from their email address #mail it mail($to, $subject, $message, $headers); echo 'Thank you for sending mail.'; } else { echo 'Sorry, one of more of your fields is invalid.';} } ?> |
| QUOTE (Josh @ Mar 20 2004, 02:34 AM) | ||||||
OK, lets say I want the user to specify the subject. Would I just add this stuff:
would be added to the javascript this would be added to the form html:
and the php would look like this:
does this look OK timo? |
| CODE |
| echo 'Sorry, one o[b]f[/b] more of your fields is invalid.';} |
| QUOTE (joe2kiss @ Mar 20 2004, 02:31 PM) | ||
| great tutorial Timo works great and easy to customize too 1 spelling mistake in your code though
|
| CODE |
<?php //change/add to this line depending on what format of image you want. $mime_type = array(1=>"image/jpeg", 2=>"image/gif", 3=>"image/jpg", 4=>"image/pjpeg"); //this part decides what type of mime type to put after the type of image is determined function mailcheck($emailadr){ return eregi("^[a-zA-Z0-9_.-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,4}$", $emailadr); } if (!isset($_POST['submit'])){ #if submit doesn't have a value ?> <form method="post" name="emailform" action="<?php echo $_SERVER['SCRIPT_NAME']?>" onSubmit="return check(this);" enctype="multipart/form-data"> <p>Name: <input type="text" name="name" style="background:#ccc;" /></p> <p>Subject: <input type="text" name="subject" style="background:#ccc;" /></p> <p>Email: <input type="text" name="email" style="background:#ccc;" /></p> Send this file: <input name="userfile" type="file" /> <p>Comments/Questions: <br /> <textarea name="comment" rows="10" cols="30" style="background:#ccc;"></textarea> </p> <p><input type="submit" name="submit" value="Submit" style="border:1px solid black; color: blue; font-family:arial; background:#ccc;" /></p> </form> <?php } else { if (is_uploaded_file($_FILES['userfile']['tmp_name'])) { // you can change this if you move the uploaded file from the tmp folder. $uploaddir = '/the/directory/here/uploads/'; // change this $uploadfile = $uploaddir . $_FILES['userfile']['name']; if(array_search($_FILES['userfile']['type'],$mime_type)){ if ($_FILES['userfile']['size'] > 100000) { echo 'file size too large'; exit; } if (!copy($_FILES['userfile']['tmp_name'], $uploadfile)) { echo "Possible file upload attack! Here's some debugging info:\n"; print_r($_FILES); exit; } $new_mimetype = $_FILES['userfile']['type']; } else { echo "Wrong file type uploaded."; exit; } } else { echo "No file Uploaded, or file was empty"; exit; } $name = $_POST['name']; $email = $_POST['email']; $subject = $_POST['subject']; $comment = stripslashes($_POST['comment']); $to = "bob@example.com"; if ((mailcheck($_POST['email']))||($comment!=NULL)||($name!=NULL)||($subject!=NULL)){ #check if a real email address, if it returns true it sends the email #also checks to make sure the comment and name fields aren't empty # subject #$subject = "mail from site" deleted # message $message = ' <html> <head> <title>$subject</title> <style type="text/css"> body { font-family: arial; background: #ccc; } </style> </head> <body> <span style="font-size:12pt;font-weight:bold;color:blue;">Subject:</span> '.$subject.'<br /><br /> <span style="font-size:12pt;font-weight:bold;color:blue;">Name of person:</span> '.$name.'<br /><br /> <span style="font-size:12pt;font-weight:bold;color:blue;">Email of person:</span> '.$email.'<br /><br /> <span style="font-size:12pt;font-weight:bold;color:blue;">Comment/Question:</span><br /> <div style="font-size:10pt;">'.$comment.'</div> <br /> </body> </html>'; // create a unique boundry, can basically be anything. // generally people use time() $mail_boundary = md5(uniqid(time())); // create header $mail_headers = "From: $email\r\n"; #The sender's email $mail_headers .= "MIME-Version: 1.0\r\n"; $mail_headers .= "Content-type: multipart/mixed; boundary=\"$mail_boundary\"\r\n\r\n"; if (file_exists($uploadfile)) { // must open the file in readonly and binary format $fp = fopen($uploadfile, "rb"); $file = fread($fp, filesize($uploadfile)); // also you have to base64 it so the email can see it. $file = chunk_split(base64_encode($file)); // must get the name of the file $filename = basename($uploadfile); } // sets the email up so we can send the text and the attachment at the same time $new_mail_body .= "--{$mail_boundary}\r\n"; $new_mail_body .= "Content-Type: text/html; charset=\"iso-8859-1\"\r\n"; $new_mail_body .= "Content-Transfer-Encoding: 7bit\r\n\r\n"; $new_mail_body .= "{$message}\r\n"; $new_mail_body .= "--{$mail_boundary}\r\n"; $new_mail_body .= "Content-Type: $new_mimetype; name=\"{$filename}\"\r\n"; $new_mail_body .= "Content-Transfer-Encoding:base64\r\n "; $new_mail_body .= "Content-Disposition: attachment; filename=\"{$filename}\"\r\n\r\n"; $new_mail_body .= $file . "\r\n"; $new_mail_body .= "--{$mail_boundary}--\r\n"; // now the good stuff as we actually send the message and the attachment. mail($to,$subject,$new_mail_body,$mail_headers); echo 'Thank you for sending mail.'; } else { echo 'Sorry, one of more of your fields is invalid.'; } } ?> |
| CODE |
<?php if (!isset($_POST['submit'])) #if submit doesn't have a value { ?> <form method="post" name="emailform" action="<?php echo $_SERVER['SCRIPT_NAME']?>" onSubmit="return check(this);"> <p>Game Name: <input type="text" name="gamename" style="background:#ccc;" /></p> <p>Real Name(first name is OK): <input name="realname" type="text" style="background:#ccc;" value=""> </p> <p title="It is OK to enter N/A if you don't want this to be public">Email: <input type="text" name="email" style="background:#ccc;" /> </p> <p>AIM Name: <input name="aimname" type="text" style="background:#ccc;" value="" /> </p> <p>Location(State): <input name="state" type="text" style="background:#ccc;" value="" /> </p> <p>Favorite TDM Map: <input name="tdm" type="text" style="background:#ccc;" value="" /> </p> <p>Favorite Obj. Map: <input name="obj" type="text" style="background:#ccc;" value=""> </p> <p>Favorite Weapon: <input name="favweapon" type="text" style="background:#ccc;" value="" /> </p> <p>2nd Favorite Weapon: <input name="ndfavweapon" type="text" style="background:#ccc;" value=""> </p> <p>Hobbies: <textarea name="hobbies" style="background:#ccc;"></textarea> </p> <p> Position in Clan: <select name="position"> <option value="leader">Clan Leader/Co-Leader</option> <option value="captain">Ladder Team Capatain/Co-Captain</option> <option value="rcon">Rcon Member</option> <option value="member">Member</option> </select> </p> <p title="For example, Call of Duty, Generals">Other Games Played: <textarea name="othergames" style="background:#ccc;"></textarea> </p> <p><input type="submit" name="submit" value="Submit" style="border:1px solid black; color: blue; font-family:arial; background:#ccc;" /></p> </form> <?php } else { $gamename = stripslashes($_POST['gamename']); $realname = $_POST['realname']; $email = $_POST['email']; $aimname = $_POST['aimname']; $state = stripslashes($_POST['state']); $tdm = $_POST['tdm']; $obj = $_POST['obj']; $favweapon = $_POST['favweapon']; $ndfavweapon = $_POST['ndfavweapon']; $hobbies = stripslashes($_POST['hobbies']); $position = $_POST['position']; $othergames = stripslashes($_POST['othergames']); $to = "person<email@place.com> "; if (($gamename!=NULL)||($realname!=NULL)||($email!=NULL)||($aimname!=NULL)||($state!=NULL)||($tdm!=NULL)|| ($obj!=NULL)||($favweapon!=NULL)||($ndfavweapon!=NULL)||($hobbies!=NULL)||($position!=NULL)||($othergames!=NULL)) #check if a real email address, if it returns true it sends the email #also checks to make sure the comment and name fields aren't empty { # subject #$subject = "mail from site" deleted # message $message = ' <html> <head> <title>$gamename</title> <style type="text/css"> body { font-family: arial; background: #ccc; } .section { font-size:12pt;font-weight:bold;color:blue; } </style> </head> <body> <span class="section">Subject:</span> '.$gamename.'<br /><br /> <span class="section">Name of person:</span> '.$realname.'<br /><br /> <span class="section">Email of person:</span> '.$email.'<br /><br /> <span class="section">Subject:</span> '.$aimname.'<br /><br /> <span class="section">Name of person:</span> '.$state.'<br /><br /> <span class="section">Email of person:</span> '.$tdm.'<br /><br /> <span class="section">Subject:</span> '.$obj.'<br /><br /> <span class="section">Name of person:</span> '.$favweapon.'<br /><br /> <span class="section">Email of person:</span> '.$ndfavweapon.'<br /><br /> <span class="section">Subject:</span> '.$hobbies.'<br /><br /> <span class="section">Name of person:</span> '.$position.'<br /><br /> <span class="section">Email of person:</span> '.$othergames.'<br /><br /> </body> </html> '; $headers = "MIME-Version: 1.0\r\n"; #headers $headers .= "Content-type: text/html; charset=iso-8859-1\r\n"; $headers .= "From: $gamename\r\n"; #The sender's email #Will show up in your box from their email address #mail it mail($to, $gamename, $realname, $email, $aimname, $state, $tdm, $obj, $favweapon, $ndfavweapon, $hobbies, $position, $othergames); echo '<p>Your email was sent.</p>'; } else { echo 'Sorry, one of more of your fields is invalid.';} } ?> |
| CODE |
<?php if (!isset($_POST['submit'])) #if submit doesn't have a value { ?> <form method="post" name="emailform" action="<?php echo $_SERVER['SCRIPT_NAME']?>" onSubmit="return check(this);"> <p>Game Name: <input type="text" name="gamename" style="background:#ccc;" /></p> <p>Real Name(first name is OK): <input name="realname" type="text" style="background:#ccc;" value=""> </p> <p title="It is OK to enter N/A if you don't want this to be public">Email: <input type="text" name="email" style="background:#ccc;" /> </p> <p>AIM Name: <input name="aimname" type="text" style="background:#ccc;" value="" /> </p> <p>Location(State): <input name="state" type="text" style="background:#ccc;" value="" /> </p> <p>Favorite TDM Map: <input name="tdm" type="text" style="background:#ccc;" value="" /> </p> <p>Favorite Obj. Map: <input name="obj" type="text" style="background:#ccc;" value=""> </p> <p>Favorite Weapon: <input name="favweapon" type="text" style="background:#ccc;" value="" /> </p> <p>2nd Favorite Weapon: <input name="ndfavweapon" type="text" style="background:#ccc;" value=""> </p> <p>Hobbies: <textarea name="hobbies" style="background:#ccc;"></textarea> </p> <p> Position in Clan: <select name="position"> <option value="leader">Clan Leader/Co-Leader</option> <option value="captain">Ladder Team Capatain/Co-Captain</option> <option value="rcon">Rcon Member</option> <option value="member">Member</option> </select> </p> <p title="For example, Call of Duty, Generals">Other Games Played: <textarea name="othergames" style="background:#ccc;"></textarea> </p> <p><input type="submit" name="submit" value="Submit" style="border:1px solid black; color: blue; font-family:arial; background:#ccc;" /></p> </form> <?php } else { $gamename = stripslashes($_POST['gamename']); $realname = $_POST['realname']; $email = $_POST['email']; $aimname = $_POST['aimname']; $state = stripslashes($_POST['state']); $tdm = $_POST['tdm']; $obj = $_POST['obj']; $favweapon = $_POST['favweapon']; $ndfavweapon = $_POST['ndfavweapon']; $hobbies = stripslashes($_POST['hobbies']); $position = $_POST['position']; $othergames = stripslashes($_POST['othergames']); $to = "person<email@place.com> "; if (($gamename!=NULL)||($realname!=NULL)||($email!=NULL)||($aimname!=NULL)||($state!=NULL)||($tdm!=NULL)|| ($obj!=NULL)||($favweapon!=NULL)||($ndfavweapon!=NULL)||($hobbies!=NULL)||($position!=NULL)||($othergames!=NULL)) #check if a real email address, if it returns true it sends the email #also checks to make sure the comment and name fields aren't empty { # subject #$subject = "mail from site" deleted # message $message = ' <html> <head> <title>$gamename</title> <style type="text/css"> body { font-family: arial; background: #ccc; } .section { font-size:12pt;font-weight:bold;color:blue; } </style> </head> <body> <span class="section">Subject:</span> '.$gamename.'<br /><br /> <span class="section">Name of person:</span> '.$realname.'<br /><br /> <span class="section">Email of person:</span> '.$email.'<br /><br /> <span class="section">Subject:</span> '.$aimname.'<br /><br /> <span class="section">Name of person:</span> '.$state.'<br /><br /> <span class="section">Email of person:</span> '.$tdm.'<br /><br /> <span class="section">Subject:</span> '.$obj.'<br /><br /> <span class="section">Name of person:</span> '.$favweapon.'<br /><br /> <span class="section">Email of person:</span> '.$ndfavweapon.'<br /><br /> <span class="section">Subject:</span> '.$hobbies.'<br /><br /> <span class="section">Name of person:</span> '.$position.'<br /><br /> <span class="section">Email of person:</span> '.$othergames.'<br /><br /> </body> </html> '; $headers = "MIME-Version: 1.0\r\n"; #headers $headers .= "Content-type: text/html; charset=iso-8859-1\r\n"; $headers .= "From: $gamename\r\n"; #The sender's email #Will show up in your box from their email address #mail it mail($to, $subject, $message, $headers); echo '<p>Your email was sent.</p>'; } else { echo 'Sorry, one of more of your fields is invalid.';} } ?> |
| CODE |
| <form method="post" name="emailform" action="<?php echo $_SERVER['SCRIPT_NAME']?>" onSubmit="return |