json - 使用PHP成功提交时未显示的表单成功消息

时间:2016-12-23 14:28:32

标签: php jquery json forms

我很难在表单上显示成功消息。它显示所有错误消息,但不显示成功消息。

在控制器文件中显示我评论$ this-> model_catalog_outofstockquery-> addQuery($ this-> request-> post); 但这条线是必要的。

在提交表单时,条目已成功加载到数据库。

我已经从构造函数中调用了模型

我在控制器文件上的代码是 -

    public function write() {
        $this->load->language('product/outofstock_enquiry');

        $json = array();

        if ($this->request->server['REQUEST_METHOD'] == 'POST') {

            if ((utf8_strlen($this->request->post['name']) < 3) || (utf8_strlen($this->request->post['name']) > 25)) {
                $json['error'] = $this->language->get('error_name');
            }
            if (!filter_var($this->request->post['email'], FILTER_VALIDATE_EMAIL)) {
                $json['error'] = $this->language->get('error_email');
            }
            if ((!filter_var($this->request->post['quantity'],FILTER_VALIDATE_INT))) {
                $json['error'] = $this->language->get('error_quantity');
            }            

                if (!isset($json['error'])) {            
                $json['success'] = $this->language->get('text_success');
                    $data['product_id'] = $this->request->post['product_id'];
                    $data['product_name'] = $this->request->post['product_name'];
                    $data['name'] = $this->request->post['name'];
                    $data['email'] = $this->request->post['email'];
                    $data['quantity'] = $this->request->post['quantity'];
                    $data['notify'] = 0;
                    $this->model_catalog_outofstockquery->addQuery($this->request->post); 
                }

        }

        $this->response->addHeader('Content-Type: application/json');
        $this->response->setOutput(json_encode($json));
    }

表单下的视图文件上的脚本是

<script type="text/javascript"><!--
  $('#button-submit').on('click', function() {
    $.ajax({
      url: 'index.php?route=product/outofstock_enquiry/write&product_id=<?php echo $product_id; ?>',
      type: 'post',
      dataType: 'json',
        data: 'name=' + encodeURIComponent($('input[name=\'name\']').val()) + '&email=' + encodeURIComponent($('input[name=\'email\']').val()) + '&product_name=' + encodeURIComponent($('input[name=\'product_name\']').val()) + '&quantity=' + encodeURIComponent($('input[name=\'quantity\']').val()) , 

      data: $("#form-ask").serialize(),
      beforeSend: function() {
        $('#button-submit').button('loading');
      },
      complete: function() {
        $('#button-submit').button('reset');
      },
      success: function(json) {
        $('.alert-success, .alert-danger').remove();

        if (json['error']) {
          $('#outofstock_enquiry').after('<div class="alert alert-danger"><i class="fa fa-exclamation-circle"></i> ' + json['error'] + '</div>');
        }
        if (json['success']) {
          $('#outofstock_enquiry').after('<div class="alert alert-success"><i class="fa fa-check-circle"></i> ' + json['success'] + '</div>');

          $('input[name=\'name\']').val('');
          $('input[name=\'email\']').val('');
          $('input[name=\'product_name\']').val('');
          $('input[name=\'quantity\']').val('');
          $('input[name=\'product_id\']').val('');                      
        }
      } 
    });
  });
//--></script>

0 个答案:

没有答案
相关问题