0

I would like to retrieve selected value from dropdown and pass it using Yii's ajaxLink function via POST method.

I can retrieve the values in the beforeSend part, for example,

array( // ajaxOptions
    'type' => 'POST',
    'beforeSend' => "function() {                                  
         start_time = jQuery('#start_time').find(':selected').val();
         end_time = jQuery('#end_time').find(':selected').val();

         this.data += '&start_time='+start_time;
         this.data += '&end_time='+end_time;
    }",
    .......

I can format the string, pass it and parse it in the controller but I'm using protection from CSRF and if I pass parameters as a single string I get "CSRF token could not be verified" error.

Looking forward to your replies.

1 Answer 1

1

Put everything in a form and submit it (via an AJAX request) - then the form will POST it as usual, and you don't have to munge any values. Alternatively, you could attach AJAX directly to the dropdown via htmlOptions, e.g.:

<form>
<?php
    echo CHtml::listBox('field', '', $fieldData,
        array('ajax' => array(
            'type' => 'POST',
            'url' => Controller :: createUrl($controllerAction),
            'dataType'=>'json',
            'success'=>'function(data) { console.log(data) }'
            ),
        )
    );  
?>

This way, the detail will get submitted via the form POST; you can do any munging you need in PHP after you've got the value submitted.

If you need a submit type item, I'd use an ajaxSubmitButton() instead of an ajaxLink(), and then you're getting all the form data.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.