简体   繁体   中英

I'm using cakephp 2.x, I need to pass data from a specific controller to a javascript file

$results = $this->Annonce->find('all', 
               array(
                     'conditions' => array('Annonce.name' => $name),
                     'recursive' => -1
      )
);

I need to use the value of $results in my javascript file ( /app/webroot/js/filenames.js )

As you cannot use a php code in .js file, what you can do is put the $results array in a javascript variable and use that in .js file.

eg

<script type="text/javascript">
    var results= <?php echo json_encode($results); ?>;
</script>

Now you can use this variable "results" in javascript file

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM