Javascript document.write inside PHP

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Frank Lovejoy
    New Member
    • Oct 2012
    • 5

    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.

    Code:
    <script type="text/javascript" src="http://www.MySite.com/TEST.php"></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

    Code:
    <?php
     $quotes = file("http://www.MySite.com/TEST.txt");
     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??
    Last edited by Rabbit; Oct 7 '12, 10:40 PM. Reason: Please use code tags when posting code.
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    Please use code tags when posting code.

    PHP code is not the same thing as javascript. You can't tell the page that you're going to use javascript and then give it PHP code.

    You can either do the whole thing in PHP, this is what I recommend because you have no need for javascript to do what you want to do.

    Alternatively, you can use Javascript to call a PHP page to return data which the Javascript then uses to write to the document. So it will be the Javascript that writes to the document. Not the PHP code. This method is called AJAX.

    Comment

    • Frank Lovejoy
      New Member
      • Oct 2012
      • 5

      #3
      Yes, I do have a need to do it specifically by this coding method!!
      I will not here get into why it must code exactly like this.
      I did already state that it works fine entirely in PHP.
      I know that this coding can be made to work because I have seen it done in a similar mode, but different application.
      I will not give that exact website here since someone might consider it as self-promoting spam.
      Google can quickly locate similar examples, or I would be glad to send anyone a similar and working code snippet!
      Even Bytes.com has one similar post, but different problem.

      My coding error is probably as simple as a misplaced or missing backslash or semicolon. Therefore, I would really appreciate it if someone with advanced PHP experience can sort out the exact coding to make this work!
      Thanks again for any help!

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        If you must do it in this way, and I see no reason it must unless this is a homework question, then use the alternate method I laid out in my previous post. You use javascript to initiate an AJAX request to a PHP page which returns the data you need. You then use javascript to read the returned data from the PHP page and write to the page.

        So when you say you've seen javascript code that calls a PHP page and writes to the document. What you are seeing is the AJAX method. It is not javascript running PHP code. Not at all, that's not possible. What you are seeing is javascript requesting information from a PHP page, the server runs PHP code to retrieve data, this data is returned to javascript, and javascript picks up and writes this data to the page.

        Here is a link to an AJAX tutorial: http://www.w3schools.com/ajax/default.asp
        Last edited by Rabbit; Oct 8 '12, 03:38 PM.

        Comment

        • Frank Lovejoy
          New Member
          • Oct 2012
          • 5

          #5
          Hello Rabbit,
          Thanks for your help and advice about using AJAX.

          I put forth some extra coding effort on my above posted problem and somehow was able to get this more complicated program to actually run!!
          YES, THE PROGRAM BELOW DOES WORK!!!
          Can anyone tell me why this longer program works, while my previous shorter program does not? Neither one uses AJAX!!
          Now, surely there must be a way to make my first program run also ! But how ? I use Internet Explorer.

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


          Code:
          <script type="text/javascript" src="http://www.MySite.com/JSPHP.php"></script>
          ............... .............

          2) This test text was saved as JSPHP.txt on my server.

          aaaa/xxxx
          bbbb/yyyy
          cccc/zzzz
          dddd/rrrr
          eeee/ssss
          ffff/tttt
          gggg/wwww
          hhhh/hhhh
          ............... ..............

          3) This code was saved as JSPHP.php on my server in the same folder as above txt file.


          Code:
          <?php
          srand((double) microtime() * 1000000);
          $readfile = file("JSPHP.txt");
          $k=rand(0, sizeof($readfile)-3);
          list($question,$answer) = split("/",$readfile[$k]);
          $answer = trim($answer);
          $question = trim($question);
          echo "document.write(\"Title<BR>Question : " . $question . "<BR>Answer : " . $answer . " \")";
          ?>

          Comment

          • Rabbit
            Recognized Expert MVP
            • Jan 2007
            • 12517

            #6
            Okay, I think I see what you're trying to do now. It's not that you're trying to run PHP code in javascript. What you're doing is running PHP code that creates javascript code.

            The reason the second version works and not the first version is your choice of string delimiters. In the first, your string is surrounded by single quotes ('). In the second, your string is surrounded by double quotes ("). Only strings that use double quotes will have variables expanded.

            Code:
            $str = "Hello World";
            echo "$str";
            
            Result:
            Hello World
            Code:
            $str = "Hello World";
            echo '$str';
            
            Result:
            $str

            Comment

            • Frank Lovejoy
              New Member
              • Oct 2012
              • 5

              #7
              Originally posted by Rabbit
              Okay, I think I see what you're trying to do now. It's not that you're trying to run PHP code in javascript. What you're doing is running PHP code that creates javascript code.

              The reason the second version works and not the first version is your choice of string delimiters. In the first, your string is surrounded by single quotes ('). In the second, your string is surrounded by double quotes ("). Only strings that use double quotes will have variables expanded.

              Code:
              $str = "Hello World";
              echo "$str";
              
              Result:
              Hello World
              Code:
              $str = "Hello World";
              echo '$str';
              
              Result:
              $str
              Hello Rabbit -
              Simply changing the echo single quotes to double quotes did not correct the problem for me. My first program will still NOT run!
              Did you actually run the first program on your own computer by merely making those two changes? Perhaps you could then somehow convey the exact 3 programs you used to me via this post, or via email?
              Thanks.

              Comment

              • Rabbit
                Recognized Expert MVP
                • Jan 2007
                • 12517

                #8
                Assuming that you have fopen wrappers enabled so that you can pass a URL to the file function, the only thing I changed was to fix the echo.
                Code:
                echo "document.write(\"start<BR>" . $quotes[$ranNum] . "<BR>end\");";

                Comment

                Working...