Note that there are some explanatory texts on larger screens.

plurals
  1. POCakePHP and AJAX to update Database without page refresh
    text
    copied!<p>I'm working with CakePHP 1.3.7 and I'm trying to do the following:</p> <p>On a given page, the user can click a link (or image, or button, doesn't matter) that passes a parameter which is saved into a database. BUT, all this, <strong>without refreshing the page</strong>.</p> <p>I've been doing some research and I believe I need to use AJAX as well to acomplish this. However, I can't find the a good example/explanation on how to do it.</p> <p>I think that the idea is to create the link using AJAX, which calls the controller/action that would receive the variable as a parameter and performs the operation to save it in its corresponding field/table of the DB.</p> <p>Does anyone have a small example of what I want to do? Or maybe point me to some tutorial that explains it... Thanks so much in advance!</p> <p><strong>EDIT</strong></p> <p>Well, thank you guys for your replies. THey're not working directly, but I think I'm getting closer to what I want. Here's what i'm doing now:</p> <p>I have this code in my view:</p> <pre><code>&lt;div id="prev"&gt;&lt;a&gt;click me&lt;/a&gt;&lt;/div&gt; &lt;div id="message_board"&gt; &lt;/div&gt; </code></pre> <p>I call this JS file:</p> <pre><code>$(document).ready(function () { $("#prev").click(function(event) { $.ajax({data:{name:"John",id:"100"}, dataType:"html", success:function (data, textStatus) {$("#message_board").html(data);}, type:"post", url:"\/galleries\/add"}); return false; }); }); </code></pre> <p>And my <strong>add</strong> action in my <strong>galleries</strong> controller looks like:</p> <pre><code>function add() { $this-&gt;autoRender = false; if($this-&gt;RequestHandler-&gt;isAjax()) { echo "&lt;h2&gt;Hello&lt;/h2&gt;"; print_r($this-&gt;data); $this-&gt;layout = 'ajax'; if(!empty($this-&gt;data)) { $fields = array('phone' =&gt; 8, 'modified' =&gt; false); $this-&gt;User-&gt;id = 6; $this-&gt;User-&gt;save($fields, false, array('phone')); } } } </code></pre> <p>When clicking on the '#prev' element, I get a response from the <strong>add</strong> action, I know because the text 'Hello' is printed inside <strong>#message_board</strong>. And it does this without refreshing the page, which is why I need. My problem is that I can't make the <strong>$.ajax()</strong> function to send any data, when it gets to the controller the <strong>$this->data</strong> is empty, so it never goes inside the <strong>if</strong> that saves the info to the database (right now it's saving just an easy thing, but I will want it to save the data that comes from the view). </p> <p>Can anyone see what am I doing wrong? How can I send the data to the controller?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload