hei,
$i=0;
while($row = $result->fetch_assoc()) {
echo "
<tr><td>".$row["Number"]."</td><td>".$row["MusikName"]." ".$row["MusikURL"]."</td></tr>";
this part works...it give me -> 1 test1 url1.de 2 test2 url2.de ...
So what I want is to pass the URL to a JavaScript Array...by doing in phpscript
$urlarray[]=$row["MusikURL"];
echo $urlarray[$i]; // gives me url1.de url2.de url3.de
i++; // to fill $urlarray[1] [2] [...] with urls
How do I pass the urls to a JavaScript Array so that I can access to those by javascriptarrayurl[1] javascriptarrayurl[2] (result should be a clear url) ... I got troubles with JSON :c
thanks in advance !!
asked Apr 7, 2016 at 20:32
killertoge
3451 gold badge4 silver badges14 bronze badges
-
" I got troubles with JSON" - what have you tried with JSON/AJAX?RomanPerekhrest– RomanPerekhrest2016年04月07日 20:36:55 +00:00Commented Apr 7, 2016 at 20:36
2 Answers 2
You can use jQuery and have something like
<?php
$returnArray = array();
while ($row = $result->fetch_assoc()) {
array_push($returnArray, $row);
}
$jsonArray = json_encode($returnArray);
?>
<script>
$(document).ready(function () {
var objArray = $.parseJSON("<?php echo $jsonArray; ?>");
for (var i = 0; i < objArray.length; i++) {
var row = objArray[i];
console.log(row);
// Now here you have access to row.Number row.MusikURL
}
});
Sign up to request clarification or add additional context in comments.
2 Comments
killertoge
@Ray and Barmar I tried it...didn't work...even if I use JSON.parseJSON... :c paste2.org/09C0JY8w here is the code of my project ... (the first php script and the javascript at the bottom is important ...Could you please help me where the mistakes are ? Anyways thanks for help !
killertoge
@Ray you can't believe how much helpful you was ...I tried to do this since 1 month ._. btw. I had also to change it to jQuery I think :c
You can use json_encode() to convert a PHP variable to its Javascript literal notation.
<script>
var urlarray = <?php echo json_encode($urlarray); ?>;
</script>
answered Apr 7, 2016 at 20:56
Barmar
789k57 gold badges555 silver badges669 bronze badges
Comments
default