PHP in Javascript.

Discussion in 'JavaScript' started by TVCokeCan, Jan 2, 2007.

  1. #1
    Is there anyway to use PHP within javascript. For example I want to do:

     
     var textarea = '<div id="'+obj.id+'_editor"><textarea id="'+obj.id+'_edit" name="'+obj.id+'" rows="4" cols="60">'+obj.innerHTML+'</textarea><input type="hidden" name="'+obj.id+'_resultid" value="<? echo $id ?>">';
     var button  = '<div><input id="'+obj.id+'_save" type="button" value="Save edit" /> or <input id="'+obj.id+'_cancel" type="button" value="Cancel edit" /></div></div>';
    
    HTML:
    And have php echo something in the value (echo) bit. How would I go around doing this? Thanks alot!
     
    TVCokeCan, Jan 2, 2007 IP
  2. ahkip

    ahkip Prominent Member

    Messages:
    9,205
    Likes Received:
    647
    Best Answers:
    0
    Trophy Points:
    310
    #2
    no you can't since php is server side and javascript is client side.
     
    ahkip, Jan 2, 2007 IP
  3. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #3
    it's possible, if you have the javascript inside the html the php will be processed inside it, for example

    
    <?
    $phptext = 'Hello World\n';
    ?>
    <script language="javascript">
    
    var text = '<?=$phptext ?>';
    
    document.write(text);
    
    </script>
    
    HTML:
    Couple of things to note, make sure you wrap the php vars in single quotes, else newlines will be literal and break the javascript, also the php is only processed once as the page loads, not everytime something changes ....
     
    krakjoe, Jan 2, 2007 IP
  4. weknowtheworld

    weknowtheworld Guest

    Messages:
    306
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    yes it's possible to use PHP in Javascript..
     
    weknowtheworld, Jan 5, 2007 IP