Calling PHP in Javascript help needed!

Discussion in 'PHP' started by DK42, Jul 7, 2009.

  1. #1
    Hi All

    Am trying to echo a PHP variable in a line of javascript but I cant get it to work, am hoping someone could help.

    I have the following line as part of a link:

    <a href="javascript:toggleLayer('NAME');"
    Code (markup):
    but need to replace NAME with a variable. Have tried this but I couldnt get it to work:

    <a href="javascript:toggleLayer('<?php echo $value['VARIABLE']; ?>');" 
    Code (markup):
    Would anyone have any ideas? Thanks!:)
     
    DK42, Jul 7, 2009 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    Do you know what's not working? The code below should produce:

    
    
    $value['VARIABLE'] = 'div_1';
    
    <a href="javascript:toggleLayer('div_1');"
    
    PHP:
     
    jestep, Jul 7, 2009 IP
  3. DK42

    DK42 Peon

    Messages:
    92
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hi jestep
    for some reason it just doesnt echo the varibale, so it just returns () in the link. Not sure why.
     
    DK42, Jul 7, 2009 IP
  4. DK42

    DK42 Peon

    Messages:
    92
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Ahhh actually, I think ive just realised why it doesnt work.

    My apologies, the above code does work. Its the actual sending of the variable that I made a mistake in!

    Sorry, but thanks!
     
    DK42, Jul 7, 2009 IP
  5. zain654321

    zain654321 Peon

    Messages:
    202
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    <script type="application/javascript">
    function addAnother() {
    jQuery('#addprodbox').append('<select name="prodArray[]" id="prodArray[]">
    <?php
    $prodlookup = "SELECT prodID, name FROM bolt_shop_prod";
    $proddata = mysql_query($prodlookup) or die('Failed to return data: ' . mysql_error());
    while($option = mysql_fetch_array($catdata)) {
    echo '<option value="' . $option['prodID']. '">' .$option['name'].'</option>';
    } ?>
    </select>');
    }
    </script>
     
    zain654321, Jul 7, 2009 IP