SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Evangelist silversurfer5150's Avatar
    Join Date
    Aug 2010
    Posts
    534
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question 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

    Silversurfer
    "Persistence is the path to perfection"

  2. #2
    Non-Member
    Join Date
    Apr 2011
    Location
    no fixed address
    Posts
    851
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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 ??

  3. #3
    SitePoint Evangelist silversurfer5150's Avatar
    Join Date
    Aug 2010
    Posts
    534
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

    thanks
    "Persistence is the path to perfection"

  4. #4
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •