January 26, 201016 yr Ok so i've built a contact form class which i don't build classes very often so if you find anything wrong inside the class please let me know however it has been tested and works great. This is how to call the full class for use Calling the class <?php include_once("contact.class.php"); $new = new contact(); $new->contactScript(); $new->ContactForm(); ?> And below is the class it self contact.class.php <?php //Create a class called contact class contact{ var $your_email = "youremail@yoursite.com"; var $subject = "Your subject"; //Main blank variables var $name = ''; var $email =''; var $message =''; var $body = ''; var $headers = ''; //Email regex var $email_check = "/^[a-z0-9]+([_\\.-][a-z0-9]+)*@([a-z0-9]+([\.-][a-z0-9]+)*)+\\.[a-z]{2,}$/i"; //Create Error Holder var $errors = array(); //Define Submit variable var $submit; function contactScript(){ if($this->submit = (isset($_POST['send'])) ? TRUE : FALSE){ //Create the same variables again with post $this->name = $_POST['name']; $this->email = $_POST['email']; $this->message = $_POST['message']; //------------Validation------------------ if(!$this->name){ $this->errors[] = "Please enter your name!"; } if($this->name){ if(preg_match('/.@/',$this->name)){ $this->errors[] = "Please use only valid characters in your name!"; } } if(!$this->email){ $this->errors[] = "Please enter your email!"; } if($this->email){ if(!preg_match($this->email_check,$this->email)){ $this->errors[] = "Please enter a valid email address!"; } } if(!$this->message){ $this->errors[] = "Please enter a message!"; } if(count($this->errors) > 0){ foreach($this->errors AS $this->error){ echo "$this->error<br />"; } }else{ //Send mail $this->body = "Name:\n\n $this->name\n\n Email:\n\n $this->email\n\n Message:\n\n $this->message"; $this->headers = "From: $this->name <$this->your_email>"; mail($this->your_email,$this->subject,$this->body,$this->headers); echo "<h2>Thank you your message has been sent</h2>"; } } //End of submit button check } //end of contact script function function ContactForm(){ echo " <form method='POST' action='".$_SERVER['PHP_SELF']."'> "; echo "<p><b>Name:</b><br /><input type='text' name='name' /></p>"; echo "<p><b>Email:</b><br /><input type='text' name='email' /></p>"; echo "<p><b>Message:</b><br /><textarea name='message' rows='5' cols='40'></textarea></p>"; echo "<p><input type='submit' name='send' value='Send Message' /></p>"; echo "</form>"; } } //End of class ?> Hope you enjoy if you find this helpful please give me a +1 i will also be adding a captcha function inside that class later down the road cheers
January 26, 201016 yr You're right, another thing I'd like to point out is the use of a class in this example. I suggest you read up on classes and objects and how to put them to use. This isn't correct oo. However, I do think its great you're trying to help
January 26, 201016 yr Author You're right, another thing I'd like to point out is the use of a class in this example. I suggest you read up on classes and objects and how to put them to use. This isn't correct oo. However, I do think its great you're trying to help Thank u very much i just never really use classes that much i mostly use functions and such lol could you tell me what part is wrong
February 7, 201016 yr Hi Just adding to the feedback left by everyone else. Lots of room for improvement, for example 'var' (although it works) is out of date and deprecated in PHP5. Unless you're using PHP4, properties and methods should be defined public, private or protected... and that's for starters. In this case the use of a class is redundant as you are just echoing out the form... It also requires the class to be modified depending on the type of form you wish to display - the idea of classes is that they can be reused. I'd advise taking a look at some existing form classes and seeing exactly how they work. BenTheDesigner
Create an account or sign in to comment