i am working on a little something, i have snipped together code from all over the place and am having trouble to get it to work.
So what im trying to do is have one page request another via ajax/json/php and query a database and then return the multiple results and print it out on the first page.
My problem is that whenever i run the client.php page, i only get back
id: [object Object] name: [object Object]
Instead of the data that i want, i know it is probably just a silly little mistake somewhere so any help would be appreciated.
And before i get grilled for using mysql instead of mysqli, its just how the code came that im using from http://openenergymonitor.org/emon/node/107
Client.php
<html>
<head>
<script language="javascript" type="text/javascript" src="jquery.js"></script>
</head>
<body>
<h2> Client example </h2>
<h3>Output: </h3>
<div id="output">this element will be accessed by jquery and this text replaced</div>
<script id="source" language="javascript" type="text/javascript">
$(function ()
{
$.ajax({
url: 'api.php', //the script to call to get data
data: "", //you can insert url argumnets here to pass to api.php
//for example "id=5&parent=6"
dataType: 'json', //data format
success: function(data) //on recieve of reply
{
var id = data[0]; //get id
var vname = data[1]; //get name
$('#output').html("<b>id: </b>"+id+"<b> name: </b>"+vname); //Set output element html
}
});
});
</script>
</body>
</html>
Api.php
<?php
$host = "localhost";
$user = "root";
$pass = "";
$databaseName = "abeiq_stock";
$tableName = "variables";
$con = mysql_connect($host,$user,$pass);
$dbs = mysql_select_db($databaseName, $con);
$query = "SELECT * FROM $tableName";
$result = mysql_query($query);
$rows = array();
while($r = mysql_fetch_assoc($result)){
$rows[] = $r; has the same effect, without the superfluous data attribute
$rows[] = array('data' => $r);
}
echo json_encode($rows);
?>