Escape JS to build php string

Hi guys,

I need to escape certain characters in the following line:

<a onclick=“$(‘#product’).submit();” id=“add_to_cart” class=“button”><span><?php echo $button_add_to_cart; ?></span></a>

so that I can insert them into a php echo,

i.e. $my_string = “<a onclick=”$(‘#product’).submit(); etc…etc"
and then
echo $my_string;

I am usually pretty decent at doing this but I am having some strange results and would really appreciate a fresh pair of eyes on it.

Kind regards


you might have to split the string up, escape the relevant bits and then concatenate the split bits.

But $my_string already contains a php section with an echo statement in it. Then when you echo $my_string you will be trying to echo an echo statement. Is that what you want to do ??

Hi thanks for your reply,
Its part of a very complex ecommerce CMS so while I don’t want to echo an echo specifically I need the data from it so I guess I would have to shorten that to $button_add_to_cart.

My problem is that I am trying to take one hyperlink button (the code above) and spit it out multiple times (looping though an array to change the id each time) so I can’t just write the HTML and echo what I need once, I need to include all of the HTML above in one php string. Does that make sense?

In other words, my controller file can perform the loop but I can only echo once in my view(template) so I need one string.


Had similar question yesterday: