Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

101
Visualizações
Get PHP object results using AJAX

I have a checkbox and the user checks it in, an AJAX call should be sent to a PHP file which returns an object. I have a SQL statement which returns data and the AJAX should get it from the PHP.

For instance the SQL returns 2 rows:

$sql = "SELECT * FROM table WHERE 1";
    if ($result = mysqli_query($con, $sql)) { $num = mysqli_num_rows($result); $arr = array();
        if ($num > 0) { $object = new stdClass();
            while ($bsi = $result-> fetch_assoc()) { 
                $object->id = $bsi['id']; $object->name = $bsi['name'];
                array_push($arr, $object);
            } die(json_encode($arr)); 
        }
    }

// Create an array, create an object. save fetched data into an object, then add the object into an array.

Let's say the above statement returns the 2 rows: [0: {id: 1, name: "name1"}, 1: {id: 2, name: "name2"} ]

AND THE AJAX CALL

$.ajax({
  enctype: "multipart/form-data", type: "POST", url: "path", dataType: 'json', contentType: false, processData: false,
  success: function(data) { console.log(data); }

BUT the console.log will display this: [0: {id: 1, name: "name1"}, 1: {id: 1, name: "name1"} ] It always shows the first element of the array.

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda