How to receive an associative array?

0

What I want to do is to receive an associative array in response to an ajax request but what I get is the following message "commerce_brandconnected to databaseArray " I receive the array but at the end of the result what I want is to get only the array to traverse it in the js file.

  

js file

$(document).ready(function(){
//alert("hola");
let selectCards =$('#cards');  
let selectEstados =  $('#estados');
let columna = 'commerce_brand';
//let selectCards = document.querySelector('#cards');

function cargarOpciones (idSelectIndependiente, columna, idSelectDependiente){
console.log(idSelectIndependiente);
console.log("entrando");



$.ajax({
        type:'POST',
        url: 'cargar_estados.php',
        data:{
                'peticion': 'idSelectIndependiente',
                'num':columna
            }
        //dataType:'json'
    })

    .done(function(lista_rep1){

        console.log("sd "+lista_rep1);

        idSelectIndependiente.empty();
        idSelectIndependiente.append('<option value="">Elige una opcion</option>');
        for (var i = 0; i < lista_rep1.length; i++) {

          idSelectIndependiente.append("<option value='lista_rep1[i][commerce_brand]'>lista_rep1[i][commerce_brand]</option>");
        //idSelectIndependiente.append('<option value="'+lista_rep1[i]['id']+'">'+lista_rep1[i][1]+'</option>');
            //$row[commerce_region]

    };

    })
    .fail(function(){
        alert('error al cargar las listas');
    });
console.log("saliendo");
}

cargarOpciones(selectCards, columna)
  

.php file

<?php
require_once 'conexion.php';

$cards = $_POST['peticion'];
$columna = $_POST['num'];
echo $columna;




function getData($idSelect = false, $columna){

    switch ($columna){
        case 'commerce_brand':
            $con = getCon();
            $query = "SELECT commerce_brand FROM 'commerc' group by commerce_brand";
            $result = $con->query($query);
            $row = $result->fetch_array(MYSQLI_ASSOC);
            //$listas = "<option value='$row[commerce_brand]'>$row[commerce_brand]</option>";
            return $row;
        break;

        default:
            $probando = "viene por defecto";
        return $probando;
    }
}

echo getData($cards,$columna);
    
asked by javier 30.10.2018 в 14:03
source

1 answer

0

That message seems to be a echo to debug the result, as if you had put something like this:

echo "commerce_brand";
...
echo "conectado a base de datos";
...
echo $array;

All these outputs influence the way in which the response of the php file will be written. It is possible that this echo is in the getCon() function.

I suggest you check the code to check where there may be exits like this, and in any case, print the answer using the function json_encode() in the function call getData() . Additionally, you should put the error message in an associative array so that the response comes in JSON file:

function getData($idSelect = false, $columna){

    switch ($columna){
        ...
        default:
            $error= array("error" => "viene por defecto");
            return $error;
    }
}

// Codificación de la salida en formato JSON
echo json_encode(getData($cards,$columna));

Thus, the answer with json_encode() would be:

{"error":"viene por defecto"}
    
answered by 30.10.2018 в 16:54