tags:

views:

989

answers:

3

Hi,

I have used cakePhp Form->input to create a Dropdown box inside JQuery using

 $('<?php echo $form->input($r['Attribute']['label'], array('name'=>$r['Attribute']['label'],'options'=>array(1,2,3)));?>').appendTo("#main #EntrySubmit");

But its showing me the error as Unterminated String literal...Why so???

A: 

You probably need to escape the single quotes in the $form output with addslashes().

--edit2

addcslashesh() adds the escaping char \ to specified characters. You have single quotes in your JS code, that's why you need to escape the existing single quotes in your php ouput.

$("#main #EntrySubmit").append('
<?php 
echo addcslashes(
   $form->input($r['Attribute']['label'], 
                array('name'=>$r['Attribute']['label'],
                      'options'=>array(1,2,3))
                ),
   "'"
   );
?>
');
stefita
can give brief me about addslashes()
Aruna
Still i am geeting the Error as Unterminated String literal $('
Aruna
what does your php output looks like?
stefita
if addcslashes is used it shows me the error as $(' <div class="input select"><label for="Gender">Gender</label><select name="Gender" id="Gender"><option value="0">1</option><option value="1">2</option><option value="2">3</option></select></div> ').appendTo("#main #EntrySubmit");
Aruna
with unterminated String literal $('
Aruna
Ah, ok, I see it now, you should use $("#main #EntrySubmit").append('<?php echo addcslashes( $form->input($r['Attribute']['label'], array('name'=>$r['Attribute']['label'], 'options'=>array(1,2,3)) ), "'" );?>');
stefita
appendTo actually expects a dom-element and you haven't selected one but you want to create one.
stefita
@stefita - firstly, there's no single quotes in his output. secondly, `appendTo()` takes a selector as an argument, not just a dom node.
nickf
A: 

The color coding provides a hint. You're nesting single quotes within single quotes. Wrap your php code in double quotes and things should improve:

$("<?php echo $form->input($r['Attribute']['label'], array('name'=>$r['Attribute']['label'],'options'=>array(1,2,3)));?>").appendTo("#main #EntrySubmit");

The nested single quotes are no longer ambiguous and are interpreted correctly.

Rob Wilkerson
if i used as u said i am getting error asmissing ) after argument list $("<div class="input select"><label for="Gender">Gender</label><select name="Gender" id="Gender"><option value="0">1</option><option value="1">2</option><option value="2">3</option></select></div>").appendTo("#main #EntrySubmit");
Aruna
the quotes inside the `<?php` and `?>` tags are evaluated separately on the server side and won't affect the javascript quotes outside it.
nickf
Good point, nickf. My bad.
Rob Wilkerson
+2  A: 

I think you might be having a problem if your PHP is outputting any line breaks. eg:

// BAD:
$('<div class="input select"><label for="Gender">
    Gender</label>').appendTo(...)

// GOOD:
$('<div class="input select"><label for="Gender">Gender</label>').appendTo(...)

If this is the case, replace the linebreaks with a space or with nothing:

// change from this:
$('<?php echo $form->input("blah", ...); ?>').appendTo(...)

// to this:
$('<?php echo str_replace(array("\r\n", "\r", "\n"), "", $form->input("blah", ...)); ?>')
    .appendTo(...)

... or if you want to keep the new lines, you'll need to escape them with a slash.

// this gives "Unterminated string literal"    
var x = 'abc
def';

// this gives you "abcdef"
var x = 'abc\
def';
nickf