Note that there are some explanatory texts on larger screens.

plurals
  1. POAction not running with CakePHP Js->submit()
    primarykey
    data
    text
    <p>I'm using CakePHP 1.3, and trying to make a simple message posting board with ajax. I'm trying to use the Js helper to submit a form on the index page, then refresh the message board's div to include the new message. This is all on a single page. </p> <p>I have previously posted on this, but I wanted to rephrase the question and include some updates. The previous question can be seen here <a href="https://stackoverflow.com/questions/3901906/how-to-use-js-submit-in-cakephp">How to use Js-&gt;submit() in CakePHP?</a></p> <p>When I came back to this project after a couple days, I immediately tested and the form worked (sort of). Submitting the form added a message to the database (it didn't display the message, but I haven't attacked that part yet). It worked 2 times, adding 2 messages. Then I opened the controller file and commented out some debug code, and it stopped working. It appears the action is not being called.</p> <p>Here is my messages_controller.php:</p> <pre><code>&lt;?php class MessagesController extends AppController { function index() { $messages = $this-&gt;Message-&gt;find('all'); $this-&gt;set('messages',$messages); } function add() { $this-&gt;autoRender = false; $this-&gt;Session-&gt;setFlash('Add action called'); if($this-&gt;RequestHandler-&gt;isAjax()) { $this-&gt;Session-&gt;setFlash('Ajax request made'); $this-&gt;layout = 'ajax'; if(!empty($this-&gt;data)) { if($this-&gt;Message-&gt;save($this-&gt;data)) { $this-&gt;Session-&gt;setFlash('Your Message has been posted'); } } } } } ?&gt; </code></pre> <p>Here is the index.ctp for my Message class </p> <pre><code>&lt;div id="guestbook" class="section_box"&gt; &lt;h3 id="toggle_guestbook"&gt;&lt;div class="toggle_arrow"&gt;&lt;/div&gt;Sign our Guestbook&lt;/h3&gt; &lt;?php echo $this-&gt;Form-&gt;create('Message'); 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( 'url' =&gt; array( 'controller' =&gt; 'messages', 'action' =&gt; 'add' ), 'update' =&gt; '#message_board' )); echo $this-&gt;Form-&gt;end(); echo $this-&gt;Js-&gt;writeBuffer(array('inline' =&gt; 'true')); ?&gt; &lt;div id="message_board"&gt; &lt;?php foreach($messages as $message) { ?&gt; &lt;div class="message"&gt; &lt;p class="message_txt"&gt; &lt;?php echo $message['Message']['text']; ?&gt; &lt;/p&gt; &lt;div&gt; &lt;div class="message_name"&gt; &lt;?php echo $message['Message']['name']; ?&gt; &lt;/div&gt; &lt;div class="message_date"&gt; &lt;small&gt; &lt;?php echo $message['Message']['date']; ?&gt; &lt;/small&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;?php } ?&gt; &lt;/div&gt; &lt;/div&gt; </code></pre> <p>When the submit button is clicked, I can see in the console that a POST is made to <a href="http://localhost/messages/add" rel="nofollow noreferrer">http://localhost/messages/add</a> with the correct data. But there doesn't appear to be a response. The flash message "Add action called" is NOT set from the controller (or any of the flash messages, for that matter) and the contents of #message_board are emptied.</p> <p>If I refresh the page at this point, the SECOND flash message appears ("Ajax request made"), and the contents of the #message_board are restored. However the new message was not saved, its the same 2 messages from before.</p> <p>I'm stumped. I have a feeling maybe there are bigger issues causing my problem, but I can't see it. Any help would be appreciated.</p>
    singulars
    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.
    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