Zo4inec
4/4/2017 - 1:42 PM

Отправка формы

Отправка формы

<form id="callback-popup-form" method="post" action="/index.php?route=common/callme">
					<input type="text" id="phone" name="phone" placeholder="8 ( ___ ) - ___ - __ - __">
					<input type="submit" id="callme-button" value="Перезвоните мне">
				</form>


<script type="text/javascript">
$(document).ready(function(){
  $(document.body).on('click','#callme-button',function(e){
    e.preventDefault();
		$.ajax({
			url: 'index.php?route=common/callme',
			type: 'post',
      data: $('#callback-popup-form').serialize(),
			dataType: 'json',
			success: function(json) {
				if (json['redirect']) {
					location = json['redirect'];
				}
				if (json['success']) {
            alert("Ваша заявка отправлена. Мы с Вами свяжемся в бижайшее время.");
            setTimeout(function() {
                // Done Functions
                $('#callback-popup-form').trigger("reset");
            }, 1000);
					//$('#hidden-loadmore').before(json['success']);
					// Need to set timeout otherwise it wont update the total
          //$('#page').val((page+1));
				}
			},
            
    });  
  });
});
</script>
//controller/common/callme.php

<?php
class ControllerCommonCallme extends Controller {
  private $error = '';

  public function index() {
  
    if($this->request->post && $this->validate($this->request->post)) {
				
        $text = sprintf($this->language->get('text_callme_mail'),(string)$this->request->post['phone']);
        $mail = new Mail();
				$mail->protocol = $this->config->get('config_mail_protocol');
				$mail->parameter = $this->config->get('config_mail_parameter');
				$mail->smtp_hostname = $this->config->get('config_mail_smtp_hostname');
				$mail->smtp_username = $this->config->get('config_mail_smtp_username');
				$mail->smtp_password = html_entity_decode($this->config->get('config_mail_smtp_password'), ENT_QUOTES, 'UTF-8');
				$mail->smtp_port = $this->config->get('config_mail_smtp_port');
				$mail->smtp_timeout = $this->config->get('config_mail_smtp_timeout');

				$mail->setTo($this->config->get('config_email'));
				$mail->setFrom('shop@3wstudio.info');
        //$this->config->get('config_email'));
				$mail->setSender(html_entity_decode($this->config->get('config_name'), ENT_QUOTES, 'UTF-8'));
				$mail->setSubject(html_entity_decode($this->language->get('text_callme_subject_mail'), ENT_QUOTES, 'UTF-8'));
				//$mail->setHtml($html);
				$mail->setText($text);
				$mail->send();
        
        $json['success'] = $this->language->get('text_callme_mail_sent');
    		$this->response->addHeader('Content-Type: application/json');
    		$this->response->setOutput(json_encode($json));
    } else {
        $json['error'] = $this->language->get('text_error_no_phone'); 
    		$this->response->addHeader('Content-Type: application/json');
    		$this->response->setOutput(json_encode($json));
    }
  }
  
  private function validate($post) {
    
    if(empty($post['phone'])) {
      $this->error = $this->language->get('text_error_no_phone');
      return 0;    
    } else {
      return 1;
    }
  }
  
}