简体   繁体   中英

php having issue with including javascript and quotes?

Here is my code:

$buffer .= '<legend>'.$thisField.'</legend><input type="text" name="'.$thisField.'" id="'.$thisField.'"/> <a href="javascript:;" onClick="mcImageManager.browse({fields : 'url_abs'});">[Pick file]</a><br /><small>360px W x 240px H</small><br /><br />';

It is breaking syntax error, unexpected T_STRING

I have tried every type of combination of single quotes, double quotes and escaping??

I am at a loss, What am I missing??

Thanks!!

Just escape your quotes inside the string with a backslash:

$buffer .= '<legend>'.$thisField.'</legend><input type="text" name="'.$thisField.'" id="'.$thisField.'"/> <a href="javascript:;" onClick="mcImageManager.browse({fields : \'url_abs\'});">[Pick file]</a><br /><small>360px W x 240px H</small><br /><br />';

See the PHP manual :

To specify a literal single quote, escape it with a backslash (\).

In such cases it's advisable to resort to HEREDOC strings, also for readability:

$buffer .= <<<END
  <legend>$thisField</legend>
  <input type="text" name="$thisField" id="$thisField"/>
  <a href="javascript:;" onClick="mcImageManager.browse({fields : 'url_abs'});">[Pick file]</a>
  <br /><small>360px W x 240px H</small><br /><br />
END;

This avoids having to escape any quotes. And you can just write $variables as-is within such a block.

As the highlighter tells you, php will fail near {fields: 'url_abs'} . You're inside a single quoted string, so you'll have to escape the single quotes inside the string: {fields: \'url_abs\'} ,

The issue is the single quotes around url_abs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM