A
A
Andrey_872016-09-18 03:16:35
PHP
Andrey_87, 2016-09-18 03:16:35

How to correctly transfer an array from php json to ajax?

Broke my head already, I'm new to this.
How to correctly transfer an array from php json to ajax?
Here is a piece of php code

$data['options'] = array();
      foreach ($this->model_catalog_product->getProductOptions($_POST['calc_id']) as $option) {
        $product_option_value_data = array();

        foreach ($option['product_option_value'] as $option_value) {
          if (!$option_value['subtract'] || ($option_value['quantity'] > 0)) {
    
            if ((($this->config->get('config_customer_price') && $this->customer->isLogged()) || !$this->config->get('config_customer_price')) && (float)$option_value['price']) {
              $price = $this->currency->format($this->tax->calculate($option_value['price'], $product_info['tax_class_id'], $this->config->get('config_tax') ? 'P' : false));
            } else {
              $price = false;
            }
            $json['calc_ids']['product_option_value_data']['product_option_value_id'][] = $option_value['product_option_value_id'];
            $json['calc_ids']['product_option_value_data']['option_value_id'][] = $option_value['option_value_id'];
            $json['calc_ids']['product_option_value_data']['name'][] = $option_value['name'];
            $json['calc_ids']['product_option_value_data']['price'][] = $price;
            $json['calc_ids']['product_option_value_data']['price_prefix'][] = $option_value['price_prefix'];
          }
        }
        $json['calc_ids']['options']['product_option_id'][] = $option['product_option_id'];
        $json['calc_ids']['options']['product_option_value'][] = $product_option_value_data;
        $json['calc_ids']['options']['option_id'][] = $option['option_id'];
        ******* $json['calc_ids']['options']['name'][] = $option['name']; *******
        $json['calc_ids']['options']['type'][] = $option['type'];
        $json['calc_ids']['options']['value'][] = $option['value'];
        $json['calc_ids']['options']['required'][] = $option['required'];
      }
        }
  
    $this->response->setOutput(json_encode($json));


here is ajax

$(document).ready(function () {

    $('#calc_id').change(function () {
    
        var calc_id = $(this).val();
    
        $('#option_id').attr('disabled', true);
        $('#option_id').html('<option>загрузка...</option>');
    
    $.ajax({
      url: 'index.php?route=module/featured/write',
      type: 'post',
      dataType: 'json',
      data: 'calc_id='+calc_id,			
      success: function (data) {
      
        var obj = eval(data);
         
                     
                var options = '';
        
                $(obj.calc_ids.options.name).each(function() {
                    *******  alert(obj.calc_ids.options.name);  *********
                        options += '<option value="">' + obj.calc_ids.options.name + '</option>';
                    });
                    	$('#option_id').html(options);
                    $('#option_id').attr('disabled', false);			
      }
    });
    
    });
});


I marked with asterisks what I'm trying to display. Just why all variables are displayed separated by commas, but displayed as many times as with an array of variables (as it should be), but that's why everything is separated by commas at once.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrey_87, 2016-09-18
@Andrey_87

Figured it out at last

var obj = eval(data);

                var options = '';
        
                $(obj.calc_ids.options.name).each(function(index) {
                      alert(data['calc_ids']['options']['name'][index]);
                        options += '<option value="">' + data['calc_ids']['options']['name'][index] + '</option>';
                    });

I didn’t tell him what to output inside the loop, and he outputted the entire loop to me, separated by commas. Added index value and everything worked. Thanks everyone for the replies.

K
Kirill Zhilyaev, 2016-09-18
@kirill_782

It needs to be an object, not an array. That is, in json, the first character should be { and not [. Put all the data in some array, for example response, and then encode it in json. I also racked my brain for a long time.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question