Filkatron Filkatron - 1 year ago 66
SQL Question

Dynamic row with JSON and PHP won't output data

I'm having trouble generating dynamic table rows with data in them. The data is being fetched from a db with a PHP script, and the client side is being handled with jQuery.
This code just displays empty table rows without the data values.

Can someone check what exactly is wrong with it?

The js script:

if(odabrano === "g"){

$.getJSON("nutritional_value.php?value=" + encodeURI(value), function (data) {
var ttr = $("<tr />");
$.each(data, function(k, v){
$("<td />").text(v * (parseFloat(uneseno, 10) / 100)).appendTo(ttr);
});
$("#tejbl").append(ttr);
});

}


The PHP script:

<?php

include 'connect.php';

//$value = $_SERVER['QUERY_STRING'];
$value = $_GET['value'];


$query = mysql_query("SELECT NAME, FAT, FIBER, SUGARS FROM ccm WHERE NAME LIKE '$value%'");
while( $run = mysql_fetch_array($query)){
/* $name = $run['NAME'];
$fat = $run['FAT'];
$fiber = $run['FIBER'];
$sugars = $run['SUGARS']; */

$results = array();
$results["name"]=$run['NAME'];
$results["fat"]=$run['FAT'];
$results["fiber"]=$run['FIBER'];
$results["sugars"]=$run['SUGARS'];
//Send it to the client in json format:
echo(json_encode($results));

}


?>

Answer Source

I think you're not passing a value for the javascript variable value

What happens if you change the getJSON javascript line to this:

$.getJSON("nutritional_value.php?value=banana",function(data){    
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download