Reputation: 9800
I'm using cakephp 3.4
I have a form to submit values using ajax.
<?= $this->Form->create(null, ['id' => 'search-form']) ?>
<?= $this->Form->control('keyword') ?>
<?= $this->Form->button(__('Search'), ['id' => 'search-submit']); ?>
<?= $this->Form->end() ?>
and sending this data to action using
$('#search-submit').click(function(event){
event.preventDefault();
$.post('/dashboard/custom-search/ajax-search',
{
data: $('#search-form').serialize()
}, function (response)
{
$('#search-result').html(response);
});
return false;
});
In ajaxSearch
action when I debug request data
debug($this->request->getData());
It gives
[
'data' => '_method=POST&keyword=world'
]
But when I try
debug($this->request->getData('keyword'));
It gives
null
How can I get serialized data in an action? or How to unserialize data in action/controller?
Upvotes: 0
Views: 129
Reputation: 1415
What you need to change is the way you are posting your serialized data to:
$.post('/dashboard/custom-search/ajax-search',
$('#search-form').serialize(),
function (response){
$('#search-result').html(response);
});
This way your getData()
will return data in expected format.
Full info about passing serialized data via jQuery.post()
can be found here: jQuery.post()
Upvotes: 1