I got the "pega" action in the Messages controller:
public function pega($id = null) { $posts = $this->Post->findById($id); foreach($posts as $pok) { $foda = $pok['love']; } $this->set('foda', $foda); $this->set('_serialize', array('foda')); }
In my layout, I am trying to make a request to catch data from the "pega" function and put it inside the html tag:
<script> var xmlhttp = new XMLHttpRequest(); var url = "http://localhost:81/booklandia/posts/pega/<?php echo $post['Post']['id'];? >.json"; xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { var out = JSON.parse(xmlhttp.responseText); function loap (){ var arr = out[0]; document.getElementById("id01").innerHTML = arr; } } } xmlhttp.open("GET", url, true); xmlhttp.send();
source share