0

I hava a problem with my ajax call and the json response. The console tell me that my php file don't return a json format but i don't understand why. Here is my ajax function :

        function showEspece(espece, categorie, object) 
  {
    $.ajax({           
      type : 'POST',                           
      url: 'getespece.php',                  
      data: {espece: espece, categorie: categorie },                       
      dataType: 'json',                     
      success: function(data)          
      {
        alert(data);
        var tableau = data;           
        $('#output').html(tableau); 
      },
      error: function(xhr, status, error) {
        console.log(xhr.responseText);
        console.log(error);
      }
    });
  }

And here is the php page call by the ajax function :

<?php
header('Content-type: application/json');
include("includes/connexionBD.php");

$requete = oci_parse($connect, "SELECT nomA, sexe, datenaissance FROM Animal WHERE categorie = '".$_POST['categorie']."' AND espece = '".$_POST['espece']."' ");

oci_execute($requete);

$test = oci_fetch_all($requete, $res);
$test1 = array();
$test1 = var_dump($res);


    echo json_encode($test1);

?>

My problem is that the ajax function always go to error and here is what i can read in the console :

    array(3) {
  ["NOMA"]=>
  array(3) {
    [0]=>
    string(6) "Chachi"
    [1]=>
    string(6) "Rafiki"
    [2]=>
    string(6) "Chakra"
  }
  ["SEXE"]=>
  array(3) {
    [0]=>
    string(1) "F"
    [1]=>
    string(1) "M"
    [2]=>
    string(1) "F"
  }
  ["DATENAISSANCE"]=>
  array(3) {
    [0]=>
    string(9) "05-MAY-15"
    [1]=>
    string(9) "07-JAN-15"
    [2]=>
    string(9) "17-SEP-17"
  }
}
null

SyntaxError: Unexpected token a in JSON at position 0
at parse (<anonymous>)
at Qb (jQuery.js:4)
at A (jQuery.js:4)
at XMLHttpRequest.<anonymous> (jQuery.js:4)

I have pass the day on it and i don't understand why it don't work. Can anyone help me ?

Karim
  • 59
  • 1
  • 1
  • 6

2 Answers2

3

You are using var_dump() wich does not return anything, but sends a string representation of the variable to the stdout. This is exactly what you are getting.

You can just encode the result of your oci_fetch_all query and send that as a json. The test variables in your code are not needed.

<?php

    header('Content-type: application/json');
    include("includes/connexionBD.php");

    $requete = oci_parse($connect, "SELECT nomA, sexe, datenaissance FROM Animal WHERE categorie = '".$_POST['categorie']."' AND espece = '".$_POST['espece']."' ");

    oci_execute($requete);

    oci_fetch_all($requete, $res);
    echo json_encode($res);
Jerodev
  • 32,252
  • 11
  • 87
  • 108
  • So what i should use instead of it ? – Karim Dec 08 '17 at 16:10
  • Just output the result of the query, I have updated my answer. – Jerodev Dec 08 '17 at 16:12
  • Thanks I have now an object with the Array, do you know how I can do in my ajax code to put this array in the div 'output' in html because my line $('#output').html(tableau); don't work ? – Karim Dec 08 '17 at 16:20
  • That should work fine. Are you sure you have a div in your DOM with the ID `output`? You might want to close this question if it is solved and create a new one for the new question. – Jerodev Dec 08 '17 at 16:24
  • Yes I have one, ok i do that – Karim Dec 08 '17 at 16:26
  • If this answer helped you to solve your question, consider marking this as the solution so other users might also find this. :) – Jerodev Dec 11 '17 at 08:27
0

You have some errors, var_dump is for print data and not to assign values to variables, your data are in $res not in $test or $test1, and please take carefully of data in your querys, check How can I prevent SQL injection in PHP?

header('Content-type: application/json');
include("includes/connexionBD.php");

$requete = oci_parse(
    $connect,
    "SELECT nomA, sexe, datenaissance FROM Animal WHERE categorie = '" . $_POST['categorie'] . "' AND espece = '"
    . $_POST['espece'] . "' "
);

oci_execute($requete);

oci_fetch_all($requete, $res);

echo json_encode($res);

For html result you can try this:

First change your ajax request

$.ajax({
  type    : 'POST',
  url     : 'getespece.php',
  data    : {espece: espece, categorie: categorie},
  dataType: 'html',    //You need to change from json to html
  success : function (data) {
    alert(data);
    var tableau = data;
    $('#output').html(tableau);
  },
  error   : function (xhr, status, error) {
    console.log(xhr.responseText);
    console.log(error);
  }
});

Then your PHP code

...
oci_execute($requete);

$test = oci_fetch_all($requete, $res);

header('Content-Type: text/html; charset=utf-8');
$table = '<table border="1">';
$rows = count($res);
for ($i = 0; $i < $rows; $i++){
    $cols = count($res[$i]);
    $table .= '<tr>';
    for ($j = 0; $j < $cols; $j++)
    {
        $table .= '<td>' . $res[$i][$j] . '</td>';
    }
    $table .= '</tr>';
}
$table .= '</table>';
echo $table;
Mauricio Florez
  • 1,112
  • 8
  • 14
  • Thanks I have know an object with the Array, do you know how I can do in my ajax code to put this array in the div 'output' in html because my line $('#output').html(tableau); don't work ? – Karim Dec 08 '17 at 16:22
  • You need parse the json to convert in a html table or send html instead of json – Mauricio Florez Dec 08 '17 at 16:25