I have ran into a strange behavior of passing JSON from PHP to jQuery.
I have some articles (Drupal CMS articles) and I need to push them into the environment. First solution worked, while the articles was pushed directly by PHP on page load. But as my work works with more than one CMS, it takes too much time to load them all, so I need to use ajax to load one specific project at time.
now, jquery looks like this:
function doLoadArticles() {
for (var i = 0; i< projectdata[1].length; i++){
$.ajax({
data: {ip:projectdata[0]["ip"], login:projectdata[0]["login"], pass:projectdata[0]["pass"], db:projectdata[0]["db"], datatype:projectdata[1][i], aj:"aj", fc:"doLoadArticles"},
type: "post",
url: "dataFunnel.php",
success: function(data){
console.log(data);
//console.log(jQuery.parseJSON( data )) ;
}
});
}
}
Then, on PHP side is this code:
function doLoadArticles(){
$projdata;
$conn = new mysqli($_POST['ip'], $_POST['login'], $_POST['pass'], $_POST['db']);
// Check connection
if ($conn->connect_error) {
die("<div style='position:absolute; top:0;left:0; background:white;'>Connection failed: " . $conn->connect_error." <br><br>Plese try reload the page</div>");
break;
}
else{}
$sql = "Select node_revision.title, node_revision.nid, node.language, field_revision_body.body_value, field_revision_body.bundle, node_revision.timestamp
from field_revision_body
LEFT JOIN node_revision ON field_revision_body.revision_id=node_revision.vid
LEFT JOIN node ON field_revision_body.revision_id=node.vid
where field_revision_body.bundle='".$_POST['datatype']."' AND node_revision.status=1 AND node.language='cs'
ORDER BY field_revision_body.revision_id DESC LIMIT 10";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// error_log("true");
// output data of each row
$j =0;
while($row_a = $result->fetch_assoc()) {
$projdata[$j]["title"] = $row_a["title"] ;
$projdata[$j]["timestamp"] = $row_a["timestamp"] ;
$projdata[$j]["bundle"] = $row_a["bundle"] ;
$projdata[$j]["body_value"] = $row_a["body_value"];
$projdata[$j]["nid"] = $row_a["nid"];
$projdata[$j]["language"] = $row_a["language"];
$j++;
}
}
echo json_encode($projdata);
}
The problem is, that the data are there, only the passing itself does not work .. (probably because of some buged conversion?)
if I do
echo print_r($projdata);
it will pass the data, but not in usable way (ignore the broken chars, that is another not related problem, the data are thare, that is what matters)
However, as it is clear, I need it in form that I can work with, so I need json. But if I use
echo json_encode($projdata);
it will pass some "broken nothing"
So is there any my mistake I am not aware of, or it is really some kind of bug in PHP implementation? (by the way, I run on PHP 5.6 on MS IIS7)
Or any other way possible, how to load data dynamically without the json conversion?
Thanks in advance