SitePoint Sponsor

User Tag List

Results 1 to 9 of 9

Hybrid View

  1. #1
    SitePoint Enthusiast epic1231's Avatar
    Join Date
    Sep 2003
    Location
    NJ
    Posts
    69
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Adding Javascript to PHP File

    hey all i did a search found a similiar topic but i need something a bit different
    My php knowledge is very small so what i am doing is designing the site in HTML and then using a HTML - PHP Converter to make a php file so that i can add in the php scripts i need.. Only problem i am running into is i need to have some javascript in there also and whenever i add it i run into a problem

    My HTML - PHP editor makes everything echo "content in here "; but i read somewhere javascript needs to be outside of php tags =- so what i tried to do is

    PHP Code:
    end of php code
    ?>
    <script>

    function openpopup(){
    var popurl=\"http://www.games4dummies.com/TOS.html\"
    winpops=window.open(popurl,\"\",\"width=800,height=600,\")
    }

          </script>

    <a href=\"javascript:openpopup()\" style=\"text-decoration: none\">
    <font color=\"#000000\">Terms-of-Service</font></a></font> </TD>";
    <?php
    start 
    new php code
    Should the two lines after </script> be in php or in the code part outside of the php? I have tried both ways didn't work either way for me so maybe someone can explain what i'm doing wrong

    Thanks,
    Bill
    Cobra-Games.net -- Facebook Games

  2. #2
    PHP manual bot bronze trophy Gaheris's Avatar
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    2,195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You may have not understood the concept of PHP, it generates output serverside which the webserver sends to the client (mostly the browser).
    Leaving PHP mode (?>) just outputs directly without it being parsed by PHP, it doesn't matter what it is. You can also output binary data, it isn't restricted.

  3. #3
    SitePoint Enthusiast epic1231's Avatar
    Join Date
    Sep 2003
    Location
    NJ
    Posts
    69
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    so what your saying is i can add javascript to php without having to block or change anything really?
    Cobra-Games.net -- Facebook Games

  4. #4
    SitePoint Wizard Young Twig's Avatar
    Join Date
    Dec 2003
    Location
    Albany, New York
    Posts
    1,355
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    As far as I understand it yes.

    So, you shouldn't be escaping those quotation marks. Unless the code is in a <?php ?> tag, it is just like regular HTML code.

    It's like using SHTML. The PHP (or SHTML) is loaded before everything else. Then it is treated like an ordinary HTML file.

    For example, if you had this:
    PHP Code:
    <html>
    <head>
    <title>
    <?php
    $cat
    ="cat";
    print(
    "$cat");
    ?>
    </title>
    </head>
    <body bgcolor="#f0f0f0">
    cat
    </body>
    </html>
    It would be broken down to:

    HTML Code:
    <html>
    <head>
    <title>
    cat
    </title>
    </head>
    <body bgcolor="#f0f0f0">
    cat
    </body>
    </html>
    then rendered as HTML code, correct?


    EDIT: Actually, that would've returned a parse error. Always helps to use your semi-colons and not look like a moron.

  5. #5
    PHP manual bot bronze trophy Gaheris's Avatar
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    2,195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes.

  6. #6
    SitePoint Enthusiast epic1231's Avatar
    Join Date
    Sep 2003
    Location
    NJ
    Posts
    69
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    lol thanks guys got it working

    i'm still learning php it's great to use but tedious to learn it seems
    Cobra-Games.net -- Facebook Games

  7. #7
    SitePoint Wizard Young Twig's Avatar
    Join Date
    Dec 2003
    Location
    Albany, New York
    Posts
    1,355
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by epic1231
    lol thanks guys got it working

    i'm still learning php it's great to use but tedious to learn it seems
    I'm still learning it, too. I've been using it for 3-4 days. It's amazing. Before, I tried using PERL and after a month of no success, I tried PHP, after I got that fwrite() crap down, I've had no problems other than typos and forgetting to set permissions.

  8. #8
    SitePoint Enthusiast epic1231's Avatar
    Join Date
    Sep 2003
    Location
    NJ
    Posts
    69
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok maybe you can help me out with this problem i overlooked something and still getting javascript error's on the page - the code is

    PHP Code:
    echo "      <script>";
    echo 
    "";
    echo 
    "function openpopup(){";
    echo 
    "var popurl=\"http://www.games4dummies.com/TOS.html\"";
    echo 
    "winpops=window.open(popurl,\"\",\"width=800,height=600,\")";
    echo 
    "}";
    echo 
    "";
    echo 
    "      </script>";
    echo 
    "";
    echo 
    "<a href=\"javascript:openpopup()\" style=\"text-decoration: none\">";
    echo 
    "      <font color=\"#000000\">Terms-of-Service</font></a></font> </TD>"
    again the reason the echo is there is because i wrote the site in HTML and then use a HTML to PHP converter to change to php so i can insert a login script into it.

    Any suggestions why this is not working

    Thanks for any help,
    Bill
    Cobra-Games.net -- Facebook Games

  9. #9
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by epic1231
    ok maybe you can help me out with this problem i overlooked something and still getting javascript error's on the page - the code is

    PHP Code:
    echo "      <script>";
    echo 
    "";
    echo 
    "function openpopup(){";
    echo 
    "var popurl=\"http://www.games4dummies.com/TOS.html\"";
    echo 
    "winpops=window.open(popurl,\"\",\"width=800,height=600,\")";
    echo 
    "}";
    echo 
    "";
    echo 
    "      </script>";
    echo 
    "";
    echo 
    "<a href=\"javascript:openpopup()\" style=\"text-decoration: none\">";
    echo 
    "      <font color=\"#000000\">Terms-of-Service</font></a></font> </TD>"
    I think there is a javascript forum that may be more appropriate to get js help. As far as you php goes, can I recomend that you take a look at the heredoc style string declaration? It would not require escaping either quote, and would clean up your code considerably:

    PHP Code:
    $script = <<<EOS
    <script>

    function openpopup(){
    var popurl="http://www.games4dummies.com/TOS.html"
    winpops=window.open(popurl,"","width=800,height=600,")
    }

    </script>
    <a href="javascript:openpopup()" style="text-decoration: none">
    <font color="#000000">Terms-of-Service</font></a></font> </TD>
    EOS;
    echo 
    $script
    Also, use of <font> is depricated, perhaps <span stlye="color: black">blah</span> would work as well for you.

    Regards,
    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.


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
  •