#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    3
    Rep Power
    0

    Javascript document.write inside php


    Can anyone please help me get this code to work?
    It works fine if I use only PHP coding in code#3.
    How can I use the "document.write" method in code#3 ?


    1) The following code was saved as TESTcall.htm. Program was called by this.

    <script type="text/javascript" src=xxxxxxxxxxxxx></script>

    ............................

    2) This test text was saved as TEST.txt in the same folder as TEST.php

    aaaa
    bbbb
    cccc
    dddd
    eeee
    ffff
    gggg
    hhhh
    .............................

    3) This code was saved as TEST.php

    <?php
    $quotes = file(xxxxxxxxxxxxxxx);
    srand((double)microtime() * 1000000);
    $ranNum = rand(0, count($quotes)-1);
    echo 'document.write(\"start<BR>". $quotes[$ranNum]; ."<BR>end\");';
    ?>

    .............................

    All 3 files are in the same folder on my php-available website server.
    I have tried all variations of single quotes and double quotes with and without the backslash inside the "document.write" but cannot get any output!
    What am I doing wrong here??
  2. #2
  3. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6352
    Welcome to the forum, there's a new user guide in my signature you may find useful.

    Using [ PHP ] and [/ PHP ] tags (without spaces) around your code will automatically color it. Look at your code:
    PHP Code:
    echo 'document.write(\"start<BR>". $quotes[$ranNum]; ."<BR>end\");'
    And now look at the corrected code:
    PHP Code:
    echo 'document.write("start<BR>' $quotes[$ranNum] . '<BR>end");'
    See the coloring difference?
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  4. #3
  5. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,316
    Rep Power
    7171
    You may want to use json_encode as well, otherwise any newlines or quotation marks in the value will break your JavaScript.
    PHP Code:
    echo 'document.write("start<br />" + ' json_encode(trim((string)$quotes[$ranNum])) . ' + "<br />end");'

    Comments on this post

    • ManiacDan agrees
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    3
    Rep Power
    0

    Thanks !


    Thank you, Thank you, Thank you, E-Oreo !
    I spent the last 2 weeks trying to get this to work.
    I even asked 2 other super brains for help.
    You were the only one to get it right!!
    Fantastic

IMN logo majestic logo threadwatch logo seochat tools logo