Welcome Guest, Not a member yet? Register   Sign In
Using "PHP" inside front end scripts
#4

Thank you both for your quick reply.  

(07-02-2015, 08:16 AM)mwhitney Wrote: I prefer to write my JavaScript to retrieve any data it needs from hidden fields or data-* attributes in the HTML, rather than making it aware of PHP variables passed from a controller.

Are you aware of any online resources describing this methodology? I tried searching for something like "using php cached variables in script", but was unsuccessful.

This is how I managed to parse cached array ($survey->answers) to js array (var answers) in my view:


PHP Code:
<?php
$js_answers 
json_encode($survey->answers);
?>



Code:
<script>
   var JSON_array = <?= $js_answers ?>;
   var answers = [];

   for (var answer in JSON_array) {
       if (JSON_array.hasOwnProperty(answer)) {
           answers.push(JSON_array[answer].answer);
       }
   }
</script>
 

But based on what you wrote, I would image this is not the most effective/scalable approach.
Reply


Messages In This Thread
RE: Using "PHP" inside front end scripts - by kamenjan - 07-02-2015, 08:47 AM



Theme © iAndrew 2016 - Forum software by © MyBB