Created
July 7, 2018 10:16
-
-
Save rrd108/390504f4a6efa23323f70a1afadbe731 to your computer and use it in GitHub Desktop.
CakePHP ajax example with jQuery
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// src/Controller/LanguagesController.php | |
namespace App\Controller; | |
use App\Controller\AppController; | |
class LanguagesController extends AppController | |
{ | |
public function index() | |
{ | |
$languages = $this->paginate($this->Languages); | |
$this->set(compact('languages')); | |
$this->set('_serialize', ['languages']); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// src/Template/Languages/search.ctp | |
echo $this->Html->script('search.js', ['block' => true]); | |
?> | |
<?= $this->Form->create($languages) ?> | |
<?= $this->Form->control('name', ['url' => 'languages/search']) ?> | |
<?= $this->Form->end() ?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(function() { | |
$('#name').on('blur', function() { | |
$.ajax({ | |
type: 'POST', | |
url: $('form').attr('action'), | |
dataType : 'json', | |
data: {name : $('#name').val()}, | |
success: function(data, textStatus, jqXHR) { | |
// do whatever you want | |
}, | |
error: function(jqXHR, textStatus, errorThrown) { | |
// do whatever you want | |
} | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment