Action does not work with CakePHP Js-> submit ()

I am using CakePHP 1.3 and am trying to create a simple message posting using ajax. I am trying to use the Js helper to submit a form on an index page and then update the bulletin board div to include a new message. It is all on one page.

I wrote about this earlier, but I wanted to rephrase the question and include some updates. The previous question can be seen here. How to use Js-> submit () in CakePHP?

When I returned to this project after a couple of days, I immediately tested it and the form worked (sort of). Submitting the form added a message to the database (it did not display the message, but I have not attacked this part yet). It worked 2 times by adding 2 posts. Then I opened the controller file and commented out some debugging code, and it stopped working. It seems the action is not being called.

Here are my controller_php.php messages:

<?php class MessagesController extends AppController { function index() { $messages = $this->Message->find('all'); $this->set('messages',$messages); } function add() { $this->autoRender = false; $this->Session->setFlash('Add action called'); if($this->RequestHandler->isAjax()) { $this->Session->setFlash('Ajax request made'); $this->layout = 'ajax'; if(!empty($this->data)) { if($this->Message->save($this->data)) { $this->Session->setFlash('Your Message has been posted'); } } } } } ?> 

Here is the index.ctp index for my message class

 <div id="guestbook" class="section_box"> <h3 id="toggle_guestbook"><div class="toggle_arrow"></div>Sign our Guestbook</h3> <?php echo $this->Form->create('Message'); echo $this->Form->input('name', array('label' => 'From:')); echo $this->Form->input('text', array('label' => 'Message:')); echo $this->Js->submit('Post Your Message', array( 'url' => array( 'controller' => 'messages', 'action' => 'add' ), 'update' => '#message_board' )); echo $this->Form->end(); echo $this->Js->writeBuffer(array('inline' => 'true')); ?> <div id="message_board"> <?php foreach($messages as $message) { ?> <div class="message"> <p class="message_txt"> <?php echo $message['Message']['text']; ?> </p> <div> <div class="message_name"> <?php echo $message['Message']['name']; ?> </div> <div class="message_date"> <small> <?php echo $message['Message']['date']; ?> </small> </div> </div> </div> <?php } ?> </div> </div> 

When the submit button is pressed, I can see on the console that the POST is done at http: // localhost / messages / add with the correct data. But there seems to be no answer. The “Add action called” pop-up message is NOT installed from the controller (or any flash message, for that matter), and the contents of #message_board are empty.

If I refresh the page at this moment, a SECOND flash ("Ajax request made") message will appear and the contents of #message_board will be restored. However, the new message was not saved, its same 2 messages were earlier.

I'm at a dead end. I have a feeling, maybe there are big problems causing my problem, but I do not see this. Any help would be appreciated.

+2
source share
4 answers

But there seems to be no answer ... and the contents of #message_board are emptied.

This is because you have not determined which action / view to perform. You must do this manually, because you have set $this->autoRender to false . You can use render() for this. More information can be found on the corresponding page of the cookbook .

If you have $this->autoRender set to true , then it will replace the contents of #message_board with the contents of add.ctp


The pop-up message “Add action called” is NOT installed from the controller (or flash messages, for that matter)

I think you need to refresh the page or part containing the $this->Session->flash() bit for flash messages to appear.

The fact that the flash message appeared when the page was refreshed means that it triggered and triggered the action.

AFAIK, you can only put / print one message from a flash key in the Messages array. A flash key is where flash messages are stored by default. Each setFlash() call will overwrite the flash message set by older calls.

Since only the second flash message was displayed, we can say that he was unable to fulfill at least one of the conditions following the second call to setFlash() in the controller. You might want to add debug($this->data) statements debug($this->data) next to the conditions related to $this->data to help you debug your problem.

You can also use debug() to find out if your application has gone through a specific action or path, as it will almost always display.

So, you can do the following to check if this condition has passed:

  if(!empty($this->data)) { debug('Passed!'); 

If "Passed!" would be printed after the form was submitted, we would know that it passed this condition.


However, a new message was not saved.

Perhaps this is due to the fact that $ data is empty or it did not pass the test. If your $data not empty, it may not have passed validation, and since your form does not display validation errors; you may never have noticed them. One way to find out if it passed the test is to do the following:

  $this->Message->set($this->data); if ($this->Message->validates()) { debug('it validated logic'); } else { debug('didn't validate logic'); } 
+2
source

Ramon solutions worked for me. Here's the updated code.

Controller Add Function

 function add() { $this->autoRender = false; if($this->RequestHandler->isAjax()) { $this->layout = 'ajax'; if(!empty($this->data)) { if ($this->Message->validates()) { if($this->Message->save($this->data)) { $this->render('/elements/message_board'); } else { debug('didn\'t validate logic'); } } } } } 

Get the view of the add form:

 <?php echo $this->Form->create('Message'); echo $this->Form->input('name', array('label' => 'From:')); echo $this->Form->input('text', array('label' => 'Message:')); echo $this->Js->submit('Post Your Message', array( 'url' => array( 'controller' => 'messages', 'action' => 'add' ), 'update' => '#message_board' )); echo $this->Form->end(); echo $this->Js->writeBuffer(array('inline' => 'true')); ?> <?php pr($this->validationErrors); ?> <div id="message_board"> <?php echo $this->element('message_board'); ?> </div> 
0
source

I tried to use the same solution as you, but it does not work. Ajax is fine when I access it directly from the URL, and I get the impression that the click does nothing. When i use

  <fieldset><legend><?php __(' Run 1');?></legend> <div id="formUpdateID"><div id="#error-message"></div> <?php $orders=array_merge($emptyarray,$orders['r1']['Order']); echo $this->Form->create('Order'); echo $this->Form->input('id', array('value'=>$orders['id'],'type' =>'hidden')); echo $this->Form->input('race_id', array('value'=> $orders['race_id'],'type' =>'hidden')); echo $this->Form->input('driver_id', array('value'=>$session->read('Auth.User.driver_id'),'type' =>'hidden')); echo $this->Form->input('run', array('value'=>$run,'type' =>'hidden')); echo $this->Form->input('pm', array('value'=>$orders['pm'],'error'=>$err[$run])); echo $this->Form->input('pr', array('value'=>$orders['pr'],'error'=>$err[$run])); echo $this->Form->input('fuel', array('value'=>$orders['fuel'],'error'=>$err[$run])); echo $this->Form->input('pit', array('value'=>$orders['pit'],'label' => __('Pit on lap '),'error'=>$err[$run])); echo $this->Form->input('tyre_type', array('value'=>$orders['tyre_type'],'error'=>$err[$run])); echo $this->Js->submit('Modify', array( 'url' => array( 'controller' => 'orders', 'action' => 'ajax_edit' ), 'update' => '#error_message' )); echo $this->Form->end(); ?> <?php pr($this->validationErrors); ?> </div></fieldset> 

and in control "orders":

  function ajax_edit($id=null){ $this->autoRender = false; if($this->RequestHandler->isAjax()) { die(debug('In Ajax')); $this->layout = 'ajax'; debug('didn\'t validate logic'); } echo 'hi'; } 

None of the messages are displayed.

I have hard JS / ajax with hard coding that this piece of code does not aim at. I copied the ajax layout in the webroot / view folder.

I see AJAX code displayed in formatted source code

 <div class="submit"><input id="submit-1697561504" type="submit" value="Modify" /></div> </form><script type="text/javascript"> //<![CDATA[ $(document).ready(function () {$("#submit-1697561504").bind("click", function (event) {$.ajax({data:$("#submit-1697561504").closest("form").serialize(), dataType:"html", success:function (data, textStatus) {$("#error_message").html(data);}, type:"post", url:"\/Webmastering\/form1C\/frame\/orders\/ajax_edit\/1"}); return false;});}); //]]> </script> 

By the way, I'm starting to get bored due to the lack of a document in cakephp and its inefficiency, to realize the task is more difficult than just posting a blog post. So thanks for your help before I start destroying my computer;)

0
source

I know this is an old topic, but I came across the same problem in my application, so now I think that Thrax was doing wrong, namely: it did not put

  echo $ this-> Js-> writeBuffer (array ('inline' => 'true')); 
to the view file (or in the layout), as Logic Artist did, so scripts for processing the click of the submit button were not generated.
0
source

All Articles