Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to use Js->submit() in CakePHP?
    primarykey
    data
    text
    <p>Im trying to create a simple Ajax form for a message board in a CakePHP application, but I can't for the life of me figure out how to properly use the Js->submit() function to submit a form via Ajax.</p> <p>Heres the form code within my view:</p> <pre><code>&lt;?php echo $this-&gt;Form-&gt;create('Message',array( 'type' =&gt; 'post', 'action' =&gt; 'add', 'onSubmit' =&gt; 'return false;' )); echo $this-&gt;Form-&gt;input('name', array('label' =&gt; 'From:')); echo $this-&gt;Form-&gt;input('text', array('label' =&gt; 'Message:')); echo $this-&gt;Js-&gt;submit('Post Your Message', array( 'action' =&gt; 'add', 'update' =&gt; '#message_board' )); echo $this-&gt;Form-&gt;end(); ?&gt; &lt;div id="message_board"&gt; ... &lt;/div&gt; </code></pre> <p>And here is the controller action:</p> <pre><code>function add() { $this-&gt;autoRender = false; if($this-&gt;RequestHandler-&gt;isAjax()) { $this-&gt;layout = 'ajax'; //THIS LINE NEWLY ADDED if(!empty($this-&gt;data)) { if($this-&gt;Message-&gt;save($this-&gt;data)) { $this-&gt;Session-&gt;setFlash('Your Message has been posted'); } } } } </code></pre> <p>Oddly, what happens when I submit the form is an exact copy of the form and its containing div is duplicated INSIDE the message_board div. Weird. </p> <p>Obviously I'm missing something (or several things). If anyone has any idea, or else knows a good source of information on how to use it, it would be much appreciated.</p> <p>Thanks.</p> <p>UPDATE: I tried adding the new line <code>$this-&gt;layout = 'ajax';</code> to the controller (see above), but it had no effect. Here is the jquery output by CakePHP, incase that might tell somebody whats going on.</p> <pre><code>$(document).ready(function () { $("#submit-707957402").bind("click", function (event) { $.ajax({ action:"add", data:$("#submit-707957402").closest("form").serialize(), dataType:"html", success:function (data, textStatus) { $("#message_board").html(data); }, type:"post", url:"\/messages" }); return false; }); }); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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