jquery - CakePHP Sending data from view to controller via ajax -


i'm new cakephp. i'm trying make simple ajax request modify part of screen. i've found several examples on web, can't mix them together! when click button btnnewphon, want send content of input txtphon sent controller admincontroller. in view, div datatoshow updated show data sent controller.

i'm there, don't know how send content of inputs phonid , phontxt controller , don't know how data in controller. here's code.

view index.ctp:

<div id="datatoshow"></div> <input id="phonid" type="text"> <input id="phontxt" type="text"> <button id="btnnewphon">new</button> 

ajax view ajax_response.ctp:

<?php echo $content; ?> 

javascript admin.js (included in view):

$(function() {   $('#btnnewphon').click(function() {     = $("#txtphon").val ;     $.ajax({       type:"post",       datatype:'json',       cache: false,       data: <<< don't know put here >>>,       url:"admin/addphon",       update:"#datatoshow"     });   }); }); 

controller admincontroller.php:

public function addphon() {       if ($this->request->is('ajax')) {         // treatment of new data here          //create current date in chosen format         $new_data = '##'.$this->request->param('phonid').'-'.$this->request->param('phontxt').'##';          //set current date content show in view         $this->set('content', $new_data);           //render spacial view ajax         $this->render('ajax_response', 'ajax');       } } 

thanks!!!

i got it.

1) in view, need add form around fields , set name attribute them, like:

<form id="gonewphon">                   <input id="phonid" name="phonid" type="text">     <input id="phontxt" name="phontxt" type="text"> </form> 

2) in js, serialize form:

data: $("#gonewphon").serialize(), 

3) in controller, use data passed parameters:

$this->request->data['phontxt'] 

now works.


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -