1

my js external script contains the following code

$(document).ready(function() {

        $('[id^="comment"]').click(function(e) {

            $(this).prop('disabled', true);

            if ($(this).val() == 'Save') {
                var $thisClicked = $(this);
                $.ajax({

                    type: "POST",
                    url:"/comment/create/",   

                    success: function(data) {
                        $thisClicked.after(data);
                        $(".form").slideDown(200);
                    }                          
                });
            } else {
                var $thisClicked = $(this);
                $.ajax({
                    type: "POST",
                    url:"/comment/delete/",
                    success: function(data) {
                        $thisClicked.prop('disabled', false);
                    }
                });
            }
        });
});

my question is what variable should I use to point to the right directory in Yii and how to pass , variables.

I trying to do the equivalent of

url: "<?php echo Yii::app()->createUrl('comment/create', array('post_id' => $data['id'], 'user_id'=> Yii::app()->user->id)); ?>",

Thank you for your help.

2
  • Are you getting an error when you try what you have above? Commented Mar 20, 2014 at 18:06
  • No I'm gettin url error. Because the url is localhost/mysite/comment/create instead of localhost/mysite/index.php/comment/create Commented Mar 20, 2014 at 19:31

1 Answer 1

2

I solve this by "hiding" the url in a data attribute:

<div id="comment" data-url="<?php echo Yii::app()->createUrl('comment/create', array('post_id' => $data['id'], 'user_id'=> Yii::app()->user->id)); ?>"></div>

In your javascript code you can access it by:

$("#comment").data('url');
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.