-1

I am quite new in this and I don't know what I am missing or not doing right here it is my php file.

file.php

session_start();

      if (!isset($_SESSION['userlevel'])){ 
         header('Location: /php/logout.php');
      }else{
        $tu =  $_SESSION['user_level'];
        $userid = $_SESSION['id'];
        $username = $_SESSION['user'];


      }

$conn = new mysqli("localhost", "root", "1234", "database");


if ($tu == 3){


$array = [];

$result = mysqli_query("SELECT COUNT(*) as nformadores FROM formador;");
if(mysqli_num_rows($result) <= 0){
    echo "Error";
}else{
    while($obj = mysqli_fetch_row($result)){
    $array['nformadores'][] = $obj;        
}
}

$result = mysqli_query("SELECT COUNT(*) as nareas FROM area;");
if(mysqli_num_rows($result) <= 0){
    echo "Error";
}else{
    while($obj = mysqli_fetch_row($result)){
    $array['nareas'][] = $obj;        
}
}

echo json_encode($array);

In my php file where I have my html code I am calling this:

$(document).ready(function(){

  $.ajax({

       url: "file.php",
       dataType:'json',

     success: function(data){

       var uno=data[0];
       var due=data[1];

           $("#query1").html(uno);
           $("#query2").html(due);


    }

  });
});

I want to write the result of the first query in the HTML element with the id="query1" and the result of the second query in the HTML element with the id="query2". And I want to do it in only one ajax call. What am I missing?

Thank you so much for your help.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
JoãoEs
  • 9
  • 7
  • ***Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php).*** [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Nov 07 '16 at 18:38
  • Have you examined the JSON when it is returned (`data`)? Can you post an example of what is returned? – Jay Blanchard Nov 07 '16 at 18:42
  • i can't use the debugger since i am getting this message failed to load resource net::ERR_BLOCKED_BY_CLIENT – JoãoEs Nov 07 '16 at 18:55
  • So - your AJAX is failing. You can't parse anything until that is fixed. Have you watched the AJAX request / response in the browser's developer tools? Have you included the jQuery library in the project? Are there any errors reported? Are you running this on a web-server? – Jay Blanchard Nov 07 '16 at 18:58
  • i had to comment code to remove that error it was due to a datepicker, my dev console shows no errors but it still seems it's not getting the response from php. Yes i have included JQuery library. And nope it's all local for now i am not running this in a web-server. Any ideas of how can i workaround this. I am blocked in this step for quite some time, so i clearly accept any other suggestions. – JoãoEs Nov 07 '16 at 19:19
  • 1
    **AJAX will not work without a web server** - it is an HTTP request. – Jay Blanchard Nov 07 '16 at 19:22
  • no no i was thinking about something else i am using xampp as my own webserver sorry for the answear – JoãoEs Nov 07 '16 at 19:27
  • And when you open the developer tools of the browser you see what? – Jay Blanchard Nov 07 '16 at 19:32
  • 1
    you also can't mix MySQL APIs. You're connecting with `mysqli_` but querying with `mysql_`. And Lord only knows where `$tu` is coming from and the HTML for it. – Funk Forty Niner Nov 07 '16 at 19:36
  • here it is it's updated...with the code i currently have – JoãoEs Nov 07 '16 at 19:43
  • Noticing the edit: Please read the entire manual http://php.net/manual/en/book.mysqli.php and its related functions. There's something you're not doing. In particular, this function `mysqli_query()`. – Funk Forty Niner Nov 07 '16 at 19:43

1 Answers1

-2

Your sending your response in an associative array. And you are reading the response as if it is an indexed array

You have

var uno=data[0];
var due=data[1];

You should reference

var uno=data['nformadores'][0];
var due=data['nareas'][0];

Or you could do the reverse, send it as an indexed array

 $array[] = $obj['nformadores'];
 $array[] = $obj['nareas'];

With this you have an indexed array

Claus
  • 532
  • 2
  • 8
  • 20
  • success: function(data){ var uno=data['nformadores']; var due=data['nareas']; $("#query1").html(uno); $("#query2").html(due); } i updated it, but it seems it still doesn't work – JoãoEs Nov 07 '16 at 18:58
  • Use the reverse option. Or recheck my answer I added [0] because you are storing the results in a nested array. – Claus Nov 07 '16 at 19:03
  • You need to echo your results from php. Not sure if you have shown all your code. echo json_encode($array); – Claus Nov 07 '16 at 19:06
  • yea that return was wrong it's already fixed, and i tried to add that [0] but still no luck ...it's like as if my Jquery is not receiving any response from PHP. ANy ideas to workaround this. Another solution? – JoãoEs Nov 07 '16 at 19:20
  • Which browser are you using? – Claus Nov 07 '16 at 19:31
  • tried it in Firefox and Chrome – JoãoEs Nov 07 '16 at 19:37
  • Ok your file.php needs some re-coding before it will work again if you are not showing all your code it is hard to tell. For one thing you are testing a variable $tu. I don't see where you communicated that to the file.php app – Claus Nov 07 '16 at 19:40