[Помогите] how to add json error

Тема в разделе "OpenCart", создана пользователем affgon, 4 фев 2017.

  1. affgon

    affgon

    Регистрация:
    19 янв 2014
    Сообщения:
    125
    Симпатии:
    412
    Код:
    public function checkusername() {
            $json = array();
            if(isset($this->request->get['username'])) {
                $this->load->model('account/customer');
                $username = $this->request->get['username'];
                $customer_username = $this->model_account_customer->getCustomerByusername($username);           
                
                if (!$customer_username) {
                    $json['success'] ='DONE';
                }                                   
    
            }
            $this->response->setOutput(json_encode($json));
        }
    Код:
     <script type="text/javascript">                 
                    $('#check-username').bind('click', function() {
                        var username=$('#username').val();
                        if(username!='') {
                            $.ajax({
                                url: 'index.php?route=account/register/checkusername&username='+username,
                                type: 'post',
                                data: $('.form input[type=\'text\']'),
                                dataType: 'json',
                                success: function(json) {
                                    $('.success, .warning, .attention, information, .error').remove();
                                    if(json['success']) {
                                        $('#notification').html('<div class="success" style="display: none;">username avliable for use <img src="catalog/view/theme/default/image/close.png" alt="" class="close" /></div>');
                                        $('.success').fadeIn('slow');
    
                                    } else {
                                        $('#notification').html('<div class="warning" style="display: none;"> Username aleady in<img src="catalog/view/theme/default/image/close.png" alt="" class="close" /></div>');
                                        $('.warning').fadeIn('slow');
                                    }
                                }
                                
                            });
                        } else {
                            $('#notification').html('<div class="warning" style="display: none;">Username must not blank<img src="catalog/view/theme/default/image/close.png" alt="" class="close" /></div>');
                            $('.warning').fadeIn('slow');
                        }
                    });
                    </script>
    if i need to get error when

    if(!preg_match("/^[a-zA-Z0-9_\-\s\x{00C0}-\x{017F}]+$/u", $this->request->post['username']))

    how to do it?

    thank
     
  2. Dotrox

    Dotrox Команда форума

    Регистрация:
    27 ноя 2012
    Сообщения:
    3.198
    Симпатии:
    1.306
    Try this:
    PHP:
    public function checkusername() {
        
    $json = array();
        if(isset(
    $this->request->get['username'])) {
            
    $username $this->request->get['username'];
            if(!
    preg_match("/^[a-zA-Z0-9_\-\s\x{00C0}-\x{017F}]+$/u"$username)) {
                
    $json['error'] ='Username not match';
            } else {
                
    $this->load->model('account/customer');
                
    $customer_username $this->model_account_customer->getCustomerByusername($username);
                if (!
    $customer_username) {
                    
    $json['success'] ='DONE';
                }
            }
           
        }
        
    $this->response->setOutput(json_encode($json));
    }
     
    affgon нравится это.
  3. affgon

    affgon

    Регистрация:
    19 янв 2014
    Сообщения:
    125
    Симпатии:
    412
    I use this code it worked
    PHP:
    public function checkusername() {
            
    $json = array();
            if(isset(
    $this->request->get['username'])) {
                
    $this->load->model('account/customer');
                
    $username $this->request->get['username'];
                
    $username_exists $this->model_account_customer->getCustomerByusername($username); 
                
    $username_unlatin = (!preg_match("/((?:[\x01-\x7F]|[\xC0-\xDF][\x80-\xBF]|[\xE0-\xEF][\x80-\xBF]{2}|[\xF0-\xF7][\x80-\xBF]{3}){1,100})./x"$username));
              
                
    $disallowed = array();
              
                
    $disalloweds explode(','$this->config->get('config_username_disallowed'));
                foreach (
    $disalloweds as $disallow) {
                    
    $disallowed[] = trim($disallow);
                }         
                
    $username_disallow in_array(strtolower($username), $disallowed);

                if (
    $username_disallow) {
                    
    $json['username_disallow'] ='DONE';
                } else if (
    $username_unlatin) {
                    
    $json['username_unlatin'] ='DONE';
                } else if (!
    $username_exists)  {
                    
    $json['username_exists'] ='DONE';
                }           

            }
            
    $this->response->setOutput(json_encode($json));
        }
    Thank for advice

    HTML:
    <script type="text/javascript"><!--             
    $('#check-username').bind('click', function() {
        var username=$('#username').val();
        if(username!='' && username.length > 4 && username.length < 26 ) {
            $.ajax({
                url: 'index.php?route=sale/customer/checkusername&token=<?php echo $token; ?>&username='+username,
                type: 'post',
                data: $('.form input[type=\'text\']'),
                dataType: 'json',
                success: function(json) {
                    $('.alert-username + .error').remove();                               
                    if(json['username_disallow']) {
                        $('.alert-username').html('<div class="warning" style="display: none;"><?php echo $error_username_disallow; ?></div>');
                        $('.warning').fadeIn('slow');
                    } else if(json['username_unlatin']) {
                        $('.alert-username').html('<div class="warning" style="display: none;"><?php echo $error_username_unlatin; ?></div>');
                        $('.warning').fadeIn('slow');
                    } else if(json['username_exists']) {
                        $('.alert-username').html('<div class="success" style="display: none;"><?php echo $text_username_avliable; ?></div>');
                        $('.success').fadeIn('slow');
                    } else {
                        $('.alert-username').html('<div class="warning" style="display: none;"><?php echo $error_username_exists; ?></div>');
                        $('.warning').fadeIn('slow');
                    }   
                }
                                
            });                       
        } else {
            $('.alert-username + .error').remove();   
            $('.alert-username').html('<div class="warning" style="display: none;"><?php echo $error_username_wordcount; ?></div>');
            $('.warning').fadeIn('slow');
        }
    });
    //--></script>
     
    Последнее редактирование: 6 фев 2017