1

I'm calling CHtml::ajaxlink like so:

        <?php echo CHtml::ajaxLink('Add to a list',
            $this->createUrl('itemList/ajaxadditem'),
            array(
                'onclick'=>'$("#addToListDialog").dialog("open"); return false;',
                'type'=>'POST',
                'update'=>'#addToListDialog',
                'data' => 'js:{"product_id" : $("#productID").val()}'
            ),
            array('id'=>'showAddToListDialog'));
    ?>

I don't know how to write the values of the AJAX options array dynamically. I'm using a workaround to get the value using JavaScript, $("#productID").val() and a hidden field.

I want to write something like:

'data' => 'js:{"product_id" : "$model->product_id"}'

But "$model->product_id" is entered as a literal string.

Can anyone give me a way to do this? My method won't actually solve the problem since I need to write this AJAX link multiple times on the fly.

5
  • Where does the $model object come from? Are you not able to do something like: 'data' => "js:{'product_id' : '{$model->product_id}'}" Commented May 13, 2013 at 19:31
  • I just tried this by writing- 'data' => 'js:{"list_id": $("#Item_list_id").val(), "product_id": '{$model->product_id}' }' it gives an error- Parse error: syntax error, unexpected '{', expecting ')' in /chroot/home/mikloswe/miklos.web44.net/html/protected/views/itemList/_ajaxadditem.php on line 32 Commented May 13, 2013 at 20:19
  • the reason you are getting that error, is because you switched the type of quotations being used. It is important that you do 'data' => "js:{'product_id' : '{$model->product_id}'}" with the outer quotes in double quotes, and the inner quotes in single quotes. Otherwise, you will need to escape your single quotations with backslashes. Commented May 13, 2013 at 21:04
  • It works! Sometimes it's the little things that catch you out. Thank you! Commented May 13, 2013 at 21:30
  • No problem! I'll post a real answer below. Commented May 13, 2013 at 21:33

1 Answer 1

0

Assuming your $model instance is available, you should be able to append it dynamically like below: 'data' => "js:{'product_id' : '{$model->product_id}'}"

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

6 Comments

Thanks for this response. Just to be clear to all that read, this was a fairly simple case of not keeping tabs on which type of quotes are in use.
Kevin - I'm having a similar problem with javascript/php syntax. I'm not sure where I'm going wrong. The php variable is written into the javascript literally- 'success'=>'js: function(data) {$("#addToListDialog$model->product_id").dialog().dialog("close");}' I've tried a number of things, but nothing works. Would you take a look? I'll post another question if you'd like. Cheers.
@goose PHP variables are only resolved when the php string is defined with double quotes like "$model->product_id", and never within single quotes like '$model->product_id'. There can also be cases where it's best to enclose the variable in curly brackets like this: "{$model->product_id}". See stackoverflow.com/questions/2596837/… for more info on that.
@goose Also, one gotcha to watch out for, is the {$("#add part of your string. You will either need a space after the { or an escape like {\$("#add otherwise, it will try to interpret ("#add as a PHP variable.
I can see that what I've posted won't work. I can't see the correct way of writing this though, given the number of nested quotes in play. I really can't see it, Javascript is not proving to be my strong point. I've re-posted here if you fancy putting me straight- stackoverflow.com/questions/16573907/…
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.