S
S
Sergey Miller2020-12-18 17:04:32
opencart
Sergey Miller, 2020-12-18 17:04:32

Opencart 3 ajax password recovery request?

I'm trying to send an Ajax request to reset my password.

In catalog/..../view/.../account/fotgotten.twig made a button Immediately Ajax request<button type="button" id="forgot">send</button>


$(document).on('click', '#forgot', function(){
$.ajax({
url: 'index.php?route=account/forgotten/validate', //отправляем запрос в catalog/controller/account/forgotten.php ф-ию validate, которая protected function - может дело в protected?
type: 'post', //зщые
data: $('#ff input[type=\'text\']'),  //данные
dataType: 'json',  //json
success: function(json) {
if (json['redirect']) {
location = json['redirect']; //если все ок, то направим на страницу входа (или любую другую укажу)
} else if (json['error']) {
if (json['error']['warning']) { 
$.jGrowl(json['error']['warning']); //показ ошибок если есть, а она всего одна может быть - есть ли такой email или нет
}
}
},
error: function(xhr, ajaxOptions, thrownError) {
alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText); // ну и Ajax ошбки запроса
}
});
});
});


catalog/controller/account/forgotten

validate function

protected function validate() {
        
        $json = array(); //создаю массив json
        
    if (!isset($this->request->post['email'])) { //если нет данных
//			$this->error['warning'] = $this->language->get('error_email');  //закомментировал стандартный функционал
            $json['error']['warning'] = $this->language->get('error_email'); //то покажем ошибку
    } elseif (!$this->model_account_customer->getTotalCustomersByEmail($this->request->post['email'])) { //проверка на существование среди email юзеров
            $json['error']['warning'] = $this->language->get('error_email'); // текст ошибки если если false
//			$this->error['warning'] = $this->language->get('error_email'); //закомментировал стандартный функционал
    }
    
    // Check if customer has been approved.
    $customer_info = $this->model_account_customer->getCustomerByEmail($this->request->post['email']);

    if ($customer_info && !$customer_info['status']) {
            $json['error']['warning'] = $this->language->get('error_approved'); //отдаем текст ошибки
//			$this->error['warning'] = $this->language->get('error_approved'); //закомментировал стандартный функционал
    }
        
        $this->response->addHeader('Content-Type: application/json'); //json
    $this->response->setOutput(json_encode($json)); //json

//		return !$this->error; //закомментировал стандартный функционал
  }


As a result, when I make a request, I get the following error

Unrecognized token '<'
parsererror
<b>Warning</b>: call_user_func_array() expects parameter 1 to be a valid callback, cannot access protected method ControllerAccountForgotten::validate() in <b>/home/d/drobenfg/test.domrobensa.ru/storage/modification/system/engine/action.php</b> on line <b>79</b>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Miller, 2020-12-18
@blackseabreathe

Decided!
Slightly corrected the validate function

public function validate() {
        
        $this->load->language('account/forgotten');

    $this->load->model('account/customer');
        
        $json = array();
        
        // Check if customer has been approved.
    $customer_info = $this->model_account_customer->getCustomerByEmail($this->request->post['email']);
        
    if (!isset($this->request->post['email']) || empty($this->request->post['email'])) {
//			$this->error['warning'] = $this->language->get('error_email');
            $json['error']['warning'] = $this->language->get('error_email');
    } elseif (!$this->model_account_customer->getTotalCustomersByEmail($this->request->post['email'])) {
            $json['error']['warning'] = $this->language->get('error_email');
//			$this->error['warning'] = $this->language->get('error_email');
    }

    elseif ($customer_info && !$customer_info['status']) {
            $json['error']['warning'] = $this->language->get('error_approved');
//			$this->error['warning'] = $this->language->get('error_approved');
    }
        
        else {
        $json['error']['warning'] = 'Письмо с инструкцией по восстановлению пароля отправлено на введенную почту. Если не пришло в течение 10 минут, проверьте папку спам';
        
        }
        
        
        
        $this->response->addHeader('Content-Type: application/json');
    $this->response->setOutput(json_encode($json));

//		return !$this->error;
  }

It remains to understand how to send the letter itself

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question