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?

4
  • 2
    Try wrapping your PHP variable in curly braces: {$model->product_id} Commented May 15, 2013 at 19:59
  • Can you show your full code? Commented May 15, 2013 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. Commented May 15, 2013 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 }) Commented May 15, 2013 at 20:13

2 Answers 2

3

A cleaner way:

'success'=>'js: function(data) {
        $("#addToListDialog'.$model->product_id.'").dialog().dialog("close");}'
Sign up to request clarification or add additional context in comments.

10 Comments

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 One IDE will ... Komodo :P
and most that do have color coding so you cant miss the variables inside blocks of text. its about the escaping really ;)
@MarZab I agree, in this case it's more convenient. Here is a discussion on the subject stackoverflow.com/questions/5605965/…
@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
|
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\");}"

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.