Im having some problems getting different variables from a JSON variable.
I do a $.post on get_shop.php file that gets data from a database, take the results and put it into a array and do a json_encode on it and prints it.
get_shop.php:
$id = $_GET["id"];
$query = "
SELECT
*
FROM shop
WHERE
id = :id
";
// The parameter values
$query_params = array(
':id' => $id
);
try
{
// Execute the query against the database
$stmt = $db->prepare($query);
$result = $stmt->execute($query_params);
}
catch(PDOException $ex)
{
// Note: On a production website, you should not output $ex->getMessage().
// It may provide an attacker with helpful information about your code.
die("Failed to run query: " . $ex->getMessage());
}
// Retrieve the user data from the database. If $row is false, then the username
// they entered is not registered.
$row = $stmt->fetch();
$database_data = array(
"name" => $row["name"],
"description" => $row["description"],
"category" => $row["category"],
"price" => $row["price"]
);
print(json_encode($database_data, JSON_UNESCAPED_UNICODE));
After that i do a console.log with the data received in shop.php (The file that makes the $.post request)
shop.php $.post sample:
$(document).ready(function(){
console.log("ID: " + id );
$.post("get_shop.php?id="+id+"",
function(data,status){
console.log(data + "
Status: " + status); )};
An example for a result:
{"name":"Hardrive 5GB","description":"Speed 10MB/s","category":"Hardrive","price":"1000"}
Now, the problem is that i want to show a part of the result (Like just "name") but it says "undefined" (I use data[name]). How do i solve this? Thanks!