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...
This can go anywhere above the PHP code, what it does is when the user clicks submit it checks to see if all the fields have data and if so allows it to submit...
PHP Code:
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> |
This can go anywhere above the PHP code, what it does is when the user clicks submit it checks to see if all the fields have data and if so allows it to submit...
PHP Code:
| 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.';} } ?> |
