When I try to draw data from a database, my javascript is returning the JSON twice. Why would my JSON return this way?
PHP:
<?php
require ('functions.inc');
dbconn(); //establish my db connection
mysql_selectdb("acts");
$query = mysql_query("SELECT * from actInfo");
while ($row = mysql_fetch_assoc($query)){
$name = $row[ActName];
}
$json=json_encode($name);
echo $json;
?>
Javascript:
function getActNames(){
if (windows.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function(){
var json = xmlhttp.responseText;
var parseV = JSON.parse(json);
$("#somediv").append(parseV);
}
xmlhttp.open("POST","PHP/actMgmt.php",true);
xmlhttp.send();
}
And I'm calling it in HTML via the following:
<p class = "button" onclick= "getActNames();return false;">Some Button </p>
My JSON call is creating 2x the requested records. SO instead of getting the following:
["act1","act2","act3"]
I am getting:
["act1,"act2","act3"]["act1","act2","act3"]
It seems that every time, its called twice.
ALSO, when I just go to the PHP page, it only returns the following like I expect:
["act1","act2","act3"]
**EDIT
var_dump($name) outputs: array(6)=>{ [0]=>string(4)"act1" [1]=> string(4)"act2" [2]=> string(4)"act3"}
**EDIT
console.log(xmlhttp.responseText) gives me:
JSON.parse: unexpected end of data
["act1","act2","act3"]
var_dump($name)and output that?