1

Is there a similar way to implement this kind of "algorithm" in the right way?

for(i=0;i<howManyCourses;i++){

            var stored = "<?php echo $buf[i] ?>";
            var option = document.createElement("option");
            option.text=stored;
            e.add(option);

        }

So I want to pass the data from the $buf array into a javascript variable. I've tried many ways but it seems like I cannot find the solution. I'm new into php and I apologise for any obvious mistake.

2

2 Answers 2

1

It should be in the same file or in another case AJAX will be the solution.

<script type="text/javascript">

       const arr = <?php echo json_encode($buf); ?>;

       for (var i = 0; i < arr.length ; i++) {
           //do something
       }

 </script>
Sign up to request clarification or add additional context in comments.

Comments

0

If you need that JS' variable buf contain the same elements as PHP's $buf, you can do the following:

var buf = <?php echo json_encode($buf); ?>;

Just keep in mind that if PHP's $buf is not an indexed array, JS' buf will be an object instead of an array.

Comments

Your Answer

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.