0

I'm working with a php code where I get variables from an input and using ajax

code:

 $.ajax({
  type: "GET",
  url: "controller/appointment/src_agenda.php",
  data: {
    function: "professional",
    variable: professional,
  },
  dataType: "html",
  fail: function( jqXHR, textStatus, errorThrown ) {
    alert(jqXHR);
  },
  error: function (jqxhr, ajaxOptions, thrownError) {
    alert(JSON.stringify(jqxhr));
    alert(jqxhr.status);
    alert(thrownError);
  },
  success: function(data){
    console.log(data);
  }
});

I get an array in the success function:

 object(mysqli_result)#3 (5) {
   ["current_field"]=>
   int(0)
   ["field_count"]=>
   int(6)
   ["lengths"]=>
   NULL
   ["num_rows"]=>
   int(76)
   ["type"]=>
   int(0)
 }

If I use php foreach it shows all the rows, but I tried javascript data.forEach and it says it doesn't exist. I also tried for(var element of data) but it shows each character as an individual.

How can I show each row from this query result in javascript?

This is the code of the model:

 $sql = "SELECT medical_agenda_id, day, `date`, time_start, time_end, hour_status_id FROM medical_agenda WHERE hour_status_id>='0' AND professional_id='".$id."' ;";
 var_dump($sql);
 $res = $this->db->query($sql);
 return $res;

I need to put the result in the success function

1
  • You need to fetch the result in PHP before returning it. Commented May 9, 2018 at 14:00

1 Answer 1

1

return $res; would return plenty of unwanted data. Rather, get the resultset's data into an associative array and echo json_encode() it.

data_type better be JSON instead of html. Because, that will let you get the minimal amount of data in the payload. And you can decide how you present them in the front end.

It should be something like follows.

while($row = mysqli_fetch_assoc($res)){//Using procedural style here
    $data[] = $row;
}

echo json_encode($data);

Plus, var_dump($sql) also should be taken out. Otherwise, that will also be included in the HTTP response, which will make the JSON string invalid.

With all the garbage out,

$sql = "SELECT medical_agenda_id, day, `date`, time_start, time_end, hour_status_id FROM medical_agenda WHERE hour_status_id>='0' AND professional_id='".$id."' ;";

//var_dump($sql); Take this out!

$res = $this->db->query($sql);

//return $res; Take this out! You will return the data by echoing it to the Web Server

while($row = mysqli_fetch_assoc($res)){//Using procedural style here
    $data[] = $row;
}

echo json_encode($data); //This is the output tho the client's request.

should do the trick.

This is the MINIMAL that you could do to get your code working. There are many many optimizations that you need to do, before you put it in production.

Sign up to request clarification or add additional context in comments.

2 Comments

It gave me on the console the data perfect, I need it now to generate links to interact with the user.
So he can see a table with the data in order with the information in the specific place. I can't understand if I must use data[date] or data.date in the javascript

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.