Ravinder Reddy
Ravinder Reddy

Reputation: 3879

Cakephp make request data contain only selected field values in JSHelper

I am trying to implement Ajax functionality in Cakephp using JsHelper. By the below code I can able to achieve the required results through Ajax.

$data = $this->Js->serializeForm(array('isForm' => false, 'inline' => true));
$this->Js->get('#PostTitle')->event('change',
    $this->Js->request(
        array('controller' => 'posts', 'action' => 'get_slug'),
            array(
                //'update' => '#PostSlug',
                'success'=>'$("#PostSlug").val(data);',
                'async' => true,
                'dataExpression' => true,
                'evalScripts' => true,
                'method' => 'post',
                'data' => $data,
        )
    )
);
echo $this->Js->writeBuffer();

My question is how to pass only selected form values as $data instead of sending all form values in serialize form.

For example I want to send only the data of single field "Title" instead of whole form.

Thanks in advance.

Upvotes: 0

Views: 528

Answers (2)

Ravinder Reddy
Ravinder Reddy

Reputation: 3879

Instead we can use the below code to post the data.

'data' => '{state_id:$("#UserStateId").val()}',

Upvotes: 2

Ravinder Reddy
Ravinder Reddy

Reputation: 3879

As an alternative I am using the below code to pass values as arguments instead of GET or POST method.

$data = $this->Js->serializeForm(array('isForm' => false, 'inline' => true));
$this->Js->get('#PostTitle')->event('change',
$this->Js->request(
    array('controller' => 'posts', 'action' => 'get_slug', $this->Form->value('Post.id'), 'Some Value'),
        array(
            //'update' => '#PostSlug',
            'success'=>'$("#PostSlug").val(data);',
            'async' => true,
            'dataExpression' => true,
            'evalScripts' => true,
            'method' => 'post',
            'data' => $data,
    )
  )
);
echo $this->Js->writeBuffer();

I am passing post_id and custom some value as parameters.

Upvotes: 0

Related Questions