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.

Expedito
  • 7,771
  • 5
  • 30
  • 43
goose
  • 2,502
  • 6
  • 42
  • 69
  • Where does the $model object come from? Are you not able to do something like: `'data' => "js:{'product_id' : '{$model->product_id}'}"` – Kevin Somers-Higgins May 13 '13 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 – goose May 13 '13 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. – Kevin Somers-Higgins May 13 '13 at 21:04
  • It works! Sometimes it's the little things that catch you out. Thank you! – goose May 13 '13 at 21:30
  • No problem! I'll post a real answer below. – Kevin Somers-Higgins May 13 '13 at 21:33

1 Answers1

0

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

Kevin Somers-Higgins
  • 957
  • 1
  • 10
  • 12
  • 1
    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. – goose May 14 '13 at 08:12
  • 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 May 15 '13 at 19:44
  • @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 http://stackoverflow.com/questions/2596837/curly-braces-in-string-in-php for more info on that. – Kevin Somers-Higgins May 15 '13 at 19:51
  • @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. – Kevin Somers-Higgins May 15 '13 at 19:57
  • 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- http://stackoverflow.com/questions/16573907/trouble-writing-php-variables-into-javascript – goose May 15 '13 at 19:58
  • Ah that might be it, I'll give it a go. – goose May 15 '13 at 19:59