PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, January 20, 2022

[FIXED] access jquery unserialized data using getData in cakephp 3

 January 20, 2022     ajax, cakephp, cakephp-3.4, jquery     No comments   

Issue

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?


Solution

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()



Answered By - Szymon
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing