SHOW:
|
|
- or go back to the newest paste.
| 1 | <?php | |
| 2 | /* | |
| 3 | * Contact Form Class | |
| 4 | */ | |
| 5 | error_reporting(E_ALL); | |
| 6 | header('Cache-Control: no-cache, must-revalidate');
| |
| 7 | header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
| |
| 8 | header('Content-type: application/json');
| |
| 9 | ||
| 10 | $admin_email = '[email protected]'; // Your Email | |
| 11 | $message_min_length = 5; // Min Message Length | |
| 12 | ||
| 13 | ||
| 14 | class Contact_Form{
| |
| 15 | function __construct($details, $email_admin, $message_min_length){
| |
| 16 | ||
| 17 | $this->name = stripslashes($details['name']); | |
| 18 | $this->email = trim($details['email']); | |
| 19 | $this->subject = '[email protected]'; // Subject | |
| 20 | $this->message = stripslashes($details['message']); | |
| 21 | ||
| 22 | $this->email_admin = $email_admin; | |
| 23 | $this->message_min_length = $message_min_length; | |
| 24 | ||
| 25 | $this->response_status = true; | |
| 26 | - | $this->response_status = 1; |
| 26 | + | |
| 27 | } | |
| 28 | ||
| 29 | ||
| 30 | private function validateEmail(){
| |
| 31 | $regex = '/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i';
| |
| 32 | ||
| 33 | if($this->email == '') {
| |
| 34 | return false; | |
| 35 | } else {
| |
| 36 | $string = preg_replace($regex, '', $this->email); | |
| 37 | } | |
| 38 | ||
| 39 | return empty($string) ? true : false; | |
| 40 | } | |
| 41 | ||
| 42 | ||
| 43 | private function validateFields(){
| |
| 44 | $this->response_status=true; | |
| 45 | // Check name | |
| 46 | if(!$this->name) | |
| 47 | {
| |
| 48 | $this->response_html .= '<p>Please enter your name</p>'; | |
| 49 | - | $this->response_status = 0; |
| 49 | + | $this->response_status = false; |
| 50 | } | |
| 51 | ||
| 52 | // Check email | |
| 53 | if(!$this->email) | |
| 54 | {
| |
| 55 | $this->response_html .= '<p>Please enter an e-mail address</p>'; | |
| 56 | - | $this->response_status = 0; |
| 56 | + | $this->response_status = false; |
| 57 | } | |
| 58 | ||
| 59 | // Check valid email | |
| 60 | if($this->email && !$this->validateEmail()) | |
| 61 | {
| |
| 62 | $this->response_html .= '<p>Please enter a valid e-mail address</p>'; | |
| 63 | - | $this->response_status = 0; |
| 63 | + | $this->response_status = false; |
| 64 | } | |
| 65 | ||
| 66 | // Check message length | |
| 67 | if(!$this->message || strlen($this->message) < $this->message_min_length) | |
| 68 | {
| |
| 69 | $this->response_html .= '<p>Please enter your message. It should have at least '.$this->message_min_length.' characters</p>'; | |
| 70 | - | $this->response_status = 0; |
| 70 | + | $this->response_status = false; |
| 71 | } | |
| 72 | } | |
| 73 | ||
| 74 | ||
| 75 | private function sendEmail(){
| |
| 76 | $mail = mail($this->email_admin, $this->subject, $this->message, | |
| 77 | "From: ".$this->name." <".$this->email.">\r\n" | |
| 78 | ."Reply-To: ".$this->email."\r\n" | |
| 79 | ."X-Mailer: PHP/" . phpversion()); | |
| 80 | ||
| 81 | if($mail) | |
| 82 | {
| |
| 83 | $this->response_status = 1; | |
| 84 | $this->response_html = '<p>Thank You!</p>'; | |
| 85 | } | |
| 86 | } | |
| 87 | ||
| 88 | ||
| 89 | function sendRequest(){
| |
| 90 | $this->validateFields(); | |
| 91 | if($this->response_status) | |
| 92 | {
| |
| 93 | $this->sendEmail(); | |
| 94 | } | |
| 95 | ||
| 96 | $response = array(); | |
| 97 | $response['status'] = $this->response_status; | |
| 98 | $response['html'] = $this->response_html; | |
| 99 | ||
| 100 | echo json_encode($response); | |
| 101 | } | |
| 102 | } | |
| 103 | $parameter_ok = isset($_POST['name']) && isset($_POST['email']) &&isset($_POST['message']) ? true:false; | |
| 104 | if($parameter_ok){
| |
| 105 | - | $contact_form = new Contact_Form($_POST, $admin_email, $message_min_length); |
| 105 | + | $contact_form = new Contact_Form($_POST, $admin_email, $message_min_length); |
| 106 | - | $contact_form->sendRequest(); |
| 106 | + | $contact_form->sendRequest(); |
| 107 | } | |
| 108 | - | ?> |
| 108 | + | else{
|
| 109 | die('paramter kurang neh<pre>'.print_r($_POST,1));
| |
| 110 | } |