Cakephp 2.3 ajax form submission

I am trying to create a simple form to send data to a database using ajax. here is my look

<?php echo $this->Html->script('jquery', FALSE); ?> <?php echo $this->Html->script('validation', FALSE); ?> <div id="success"></div> <h2>Contact Us</h2> <?php echo $this->Form->create(); echo $this->Form->input('name', array('id'=>'name')); echo $this->Form->input('email', array('id'=>'email')); echo $this->Form->input('message', array('id'=>'message')); echo $this->Js->submit('Send', array( 'before'=>$this->Js->get('#sending')->effect('fadeIn'), 'success'=>$this->Js->get('#sending')->effect('fadeOut'), 'update'=>'#success' )); echo $this->Form->end(); ?> <div id="sending" style="display: none; background-color: lightgreen;">Sending...</div> 

controller

  class MessagesController extends AppController{ public $helpers = array('Js'); public $components = array('RequestHandler'); public function index(){ if(!empty($this->data)){ if($this->Message->save($this->data)){ if($this->request->isAjax()){ $this->render('success','ajax'); }else{ $this->Session->setFlash('Message sent'); $this->redirect(array('action'=>'index')); } } } } } 

it does not submit the form via ajax .. i'm actually new to cakephp and this is my first time i use ajax in cakephp. and also i want to know that i can use codeigniter or simple php similar syntax of form submission in php of cake. for example this

  <script> $('#btn').click(function(event) { form = $("#form").serialize(); $.ajax({ type: "POST", url: "<?php echo site_url('categoryController/addCategory'); ?>", data: form, success: function(data){ $('.modal').modal('hide'); $(".success").fadeIn(500).delay(2000).fadeOut(500); $("#form")[0].reset(); //Unterminated String constant fixed } }); event.preventDefault(); return false; //stop the actual form post !important! }); </script> 
+4
source share
1 answer

I got my answer. I missed the jquery.js file.

0
source

Source: https://habr.com/ru/post/1486055/


All Articles