SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Oct 2008
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP within JS won't work!

    I know there must be a simple answer to this, but I've tried everything and can't figure it out...

    I'm trying include some extremely simple php code within a Javascript file:
    Code:
    alert("<?PHP echo('hello'); ?>");
    For some reason, this generates an empty alert box. I've tried a bunch of different things, and I can't get Javascript to recognize embeded PHP code.

    Can anyone tell me what I'm missing?

  2. #2
    SitePoint Addict joaquin_win's Avatar
    Join Date
    Jul 2005
    Location
    Venezuela
    Posts
    224
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Seems ok, echo doesn't need the parentheses (although I don't think that's the problem)

  3. #3
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    If the php is within an external javascript file then it won't be processed by the php parser.

    This is perhaps one of the few occasions where inline javascript must be used.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  4. #4
    SitePoint Evangelist ferrari_chris's Avatar
    Join Date
    Mar 2008
    Location
    NP, New Zealand
    Posts
    576
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by pmw57 View Post
    If the php is within an external javascript file then it won't be processed by the php parser.

    This is perhaps one of the few occasions where inline javascript must be used.
    Is this down to the fact that the page extension when served won't have the .php extension?

  5. #5
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    That's something that the people in the PHP forum would have a better chance of providing a suitable answer.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  6. #6
    SitePoint Mentor NightStalker-DNS's Avatar
    Join Date
    Jul 2004
    Location
    Cape Town, South Africa
    Posts
    2,880
    Mentioned
    48 Post(s)
    Tagged
    0 Thread(s)
    Im not a PHP boffin, but shouldnt it be:

    alert("<?PHP echo 'hello'; ?>");

    and Yes, JS pages do not run thru PHP.

    Best thing to do if you have it as a seperate script is do this:

    On the php page:
    var phpText = <?php echo 'hello' ?>
    <script language="javascript" type="text/js" src="watever.js"></script>

    then in the js file just alert(phpText);

    I hope this helps


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
  •