I try to pass json-encoded data from my PHP- and MySQL-script back to the page with the ajax-script. The data would have multiple lines, meaning row1, row2 and so on. I pass the data back to the page and want to address the first row/line before I go on to the second and so on. But I can't even address a single one. Not only are urls looking "strange" (instead of slash / I see backslash and slash /), but I can't even get a single result if I address data[1] or else. I used to try several solutions from several posts here without any success.
Here's the PHP-part which is working fine:
$data = array();
$query = "SELECT * FROM `episode_elements` WHERE eid = 1 ORDER BY rank";
$result = mysqli_query($con, $query);
$data = array();
while($row = mysqli_fetch_array($result))
{
//echo $row['audio'];
//printf ("%s (%s)\n",$row[0],$row[1],$row[2],$row[3],$row[4]);
$data[0] = $row['audio'];
$data[1] = $row['image'];
$data[2] = $row['text'];
$data[3] = $row['rank'];
print json_encode($data);
}
Just getting the result from this script is showing a great result:
["http:\/\/www.nasa.gov\/mp3\/581097main_STS-1_Dust-it-Off.mp3","https:\/\/cdn.pixabay.com\/photo\/2011\/12\/14\/12\/11\/astronaut-11080_960_720.jpg","NASA","1"]
["http:\/\/www.mostfreebies.com\/PatrioticNostalgicMusic\/music\/USAFB_TheStarSpangledBanner.mp3","https:\/\/cdn.pixabay.com\/photo\/2013\/01\/16\/05\/08\/flag-75047_960_720.jpg","Star Spangled Banner","2"]
But there you can already see the backslashes. But then when I pass the data back to my script I can't access it. I can't see data[0] or data[1][1].
How do I fix it? Frankly spoken, JS and Ajax drive me crazy.
function load_data(limit, start)
{
$.ajax({
url:"episode_content_load.php",
method:"POST",
dataType: 'json',
data:{limit:limit, start:start},
cache:false,
success:function(data)
{
$('#load_data').append(data);
alert(data[1]);
}
});
}
While $('#load_data').append(data); works if I don't add the line datatype: JSON, alert(data[1]); doesn't show me anything. And this is confusing as it seems to work for others in similar posts.
If you have an idea, please let me know. Thanks in advance.