Problem combining PHP with JavaScript

Discussion in 'PHP' started by MartinGr, Aug 14, 2008.

  1. #1
    Problem combining PHP with JavaScript
    What I am trying to do is to add php-function-generated argument to JavaScript function. The point of the function is to give user a possibility to add dropdown boxes to the form (for additional values).
     echo '<a href="javascript:;" onClick="function(\''.function().'\')">';
    PHP:
    Function() returns a list of <option value="int">string</option>.
    The problem is, that I run out of possible quotes (meaning, the first double quotes of option value will end the ones of onClick. I have also slashed out one set of single quotes in JavaScript function, so I can’t use them either.
     
    MartinGr, Aug 14, 2008 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    
    echo '<a href="javascript:;" onClick="function(\'' . function() . '\');">';
    
    PHP:
     
    nico_swd, Aug 14, 2008 IP
  3. MartinGr

    MartinGr Peon

    Messages:
    38
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Yes, sorry, that was mistake I made when writing the example line to that post. I my real code the slashes weren't reverses.
    Thank you for pointing that out, I'll fix it in the first post.
     
    MartinGr, Aug 14, 2008 IP
  4. annanie2008

    annanie2008 Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    i think fix as:
    echo '<a href="javascript:;" onClick="function(\\'' . function() . '\\');">';
     
    annanie2008, Aug 15, 2008 IP
  5. gustavorg

    gustavorg Active Member

    Messages:
    37
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    61
    #5
    Just a suggestion. Forget that and use the <?php ?>. That is the way I use all the time, and, by the way, the coding style everybody use (at least the coders behind Wordpress, Joonla, Drupal and so :))

    So the call will be something like this

    
    <a href="javascript: onClick="<?php echo "function('function()')"; ?>;">
    
    Code (markup):
     
    gustavorg, Aug 15, 2008 IP
  6. XenFen

    XenFen Banned

    Messages:
    48
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    <?php ?> is the best to use as all servers accept those tags.
     
    XenFen, Aug 15, 2008 IP