1
 'success'=>'js: function(data) {
        $("#addToListDialog$model->product_id").dialog().dialog("close");}'

I'm trying to write the above piece of javascript, but can't get the right combination of curly brackets a single/double quotes in order to resolve the PHP variable. How should I be writing this?

goose
  • 2,502
  • 6
  • 42
  • 69
  • 2
    Try wrapping your PHP variable in curly braces: `{$model->product_id}` – andrewsi May 15 '13 at 19:59
  • Can you show your full code? – Luke Pittman May 15 '13 at 19:59
  • 1
    You shouldn't be building JavaScript with PHP. It's A Bad Idea™. Keep your HTML in `.html` files, your CSS in `.css` files, and your JS in `.js` files. Use `[data-*]` attributes and classes if you need to pass data to JavaScript. – zzzzBov May 15 '13 at 20:00
  • 1
    why are you closing the dialog as soon as you create it. if you don't want it auto opened then just use `_id').dialog({ autoOpen: false })` – SpYk3HH May 15 '13 at 20:13

2 Answers2

3

A cleaner way:

'success'=>'js: function(data) {
        $("#addToListDialog'.$model->product_id.'").dialog().dialog("close");}'
MarZab
  • 2,543
  • 21
  • 28
  • I prefer this approach as it is cleared that the string contains a PHP variable. Embedding variables inside strings is easy to overlook, which makes it hard to debug. Also, when refactoring (e.g. Rename `$model` to `$somethingElse`), most IDEs will not rename the variable if it is within a string. – thaJeztah May 15 '13 at 20:05
  • @thaJeztah One IDE will ... Komodo :P – SpYk3HH May 15 '13 at 20:10
  • and most that do have color coding so you cant miss the variables inside blocks of text. its about the escaping really ;) – MarZab May 15 '13 at 20:15
  • @MarZab I agree, in this case it's more convenient. Here is a discussion on the subject http://stackoverflow.com/questions/5605965/php-concatenate-or-directly-insert-variables-in-string – Kevin Somers-Higgins May 15 '13 at 20:16
  • @SpYk3HH Maybe should have left out 'most' LOL, *my* IDE does it as well, but I've had situations where an IDE *didn't* properly pick up the variable – thaJeztah May 15 '13 at 20:16
  • @KevinHiggins to simplify the quoting, HEREDOC notation comes in handy – thaJeztah May 15 '13 at 20:21
  • @thaJeztah That's true as well. They do have a tendency to ruin my indentation feng shui though. – Kevin Somers-Higgins May 15 '13 at 20:24
  • @KevinHiggins +1 really hoping one day HEREDOC can be used **with** indentation, some kind of 'custom' quotes. – thaJeztah May 15 '13 at 20:27
  • @thaJeztah That would be glorious. – Kevin Somers-Higgins May 15 '13 at 20:30
  • and just what PHP needs, more compatibility breaking syntax and more parser complexity. whats wrong with IDE plugins, eh? – MarZab May 15 '13 at 20:33
1

Your PHP string must be in double quotations.

This should do the trick:

'success'=>"js: function(data) {
        $(\"#addToListDialog{$model->product_id}\").dialog().dialog(\"close\");}"
Kevin Somers-Higgins
  • 957
  • 1
  • 10
  • 12