0

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
1
  • " I got troubles with JSON" - what have you tried with JSON/AJAX? Commented Apr 7, 2016 at 20:36

2 Answers 2

1

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
 }
});
answered Apr 7, 2016 at 20:52
Sign up to request clarification or add additional context in comments.

2 Comments

@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 !
@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
1

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

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.