Digipng Digipng - 1 year ago 86
Ajax Question

How to send data using ajax in cakephp3?

I working cakephp3. I have two dropdown select. The first menu list values is displayed with the loading page and then The second menu list values will be filled with the user's choice from the first menu. I do this with ajax but I don't how to send/get data to/from ajax.

In view (addroom.ctp) :

<form method="post" action="#" name="setRoom">
echo '<select name = "dorm" id="dorm" >';
foreach ($row as $a) {

echo '<option>' . "$a" . '</option>';
echo '</select>';



In ajax.js in webroot:

$(document).ready(function () {
$('#dorm').change(function () {
var dorm = $('#dorm').val();
if (dorm == '') {
$('#roomm').html("choose dormitory!");
url: "addroom.ctp",
type: 'POST',
data: dorm,
success: function (data) {


In Controller:

class UesrmoreinfoesController extends AppController {

public function addroom() {
$gender = $this->request->Session()->read('Auth.User.gender');
$url = $this->params['url'];
$dorm = $url['dorm'];


The function
in the first menu list and
in the second menu list.

The first menu data are displayed correctly.
How to get value from the first dropdown and post for ajax?

Answer Source
  1. Create form with form helper
  2. Change url: "addroom.ctp" to url: $("form").attr("action");