$.ajax({
type:'GET',
url:'index.php',
data: {val1},
dataType: 'json',
success: function(response) {
for(i=0;i<response.length;i++) {
var test = response[i];
$("#demo").append(test);
console.log(test);
//$("demo").append(test[i]);
}
console.log(response);
}
});
I have an input text field for mysql. So how can i output the json table from database through ajax?
Asked
Active
Viewed 87 times
0

GG.
- 21,083
- 14
- 84
- 130
1 Answers
0
In ur site, if u call "index.php", you get your json array correctly? if yes, in the "index.php" page did you set the header to output a json type text, like that
header("Content-type: application/json; charset=utf-8'");
? Otherwise the result page will be a normal text/html page, so your val(1) wouldn't be recognized like a json string.
If this doesn't work please try to post your "index.php" page

Came19xx
- 72
- 7
-
I have an table in my database with id, name, lastname, adress columns. My val1 is an sql input like "SELECT id, name FROM popis" and i wanna to show then just id and name column but not like this "$("#demo").html(test.id + ' ' + test.name);, i made this without json but i wanna do it now with json. Im new in this so i dont understand that so good. – Apr 18 '16 at 09:20
-
In the function you use to retrieve data from your DB, you can use a code like `header("Content-type: application/json; charset=utf-8'"); echo json_encode($return_arr);` so the result in the page is a json array, that can be recognized from your previus script like json datatype, otherwise without the header your ajax script recognize the index.php return value like a normal string – Came19xx Apr 19 '16 at 08:52