473,387 Members | 1,863 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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.

Expand|Select|Wrap|Line Numbers
  1. <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

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  $quotes = file("http://www.MySite.com/TEST.txt");
  3.  srand((double)microtime() * 1000000);
  4.  $ranNum = rand(0, count($quotes)-1);
  5. echo 'document.write(\"start<BR>". $quotes[$ranNum]; ."<BR>end\");';
  6. ?>
.............................

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??
Oct 7 '12 #1
7 6491
Rabbit
12,516 Expert Mod 8TB
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.
Oct 7 '12 #2
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!
Oct 8 '12 #3
Rabbit
12,516 Expert Mod 8TB
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
Oct 8 '12 #4
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.


Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript" src="http://www.MySite.com/JSPHP.php"></script>
  2.  
............................

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.


Expand|Select|Wrap|Line Numbers
  1. <?php
  2. srand((double) microtime() * 1000000);
  3. $readfile = file("JSPHP.txt");
  4. $k=rand(0, sizeof($readfile)-3);
  5. list($question,$answer) = split("/",$readfile[$k]);
  6. $answer = trim($answer);
  7. $question = trim($question);
  8. echo "document.write(\"Title<BR>Question : " . $question . "<BR>Answer : " . $answer . " \")";
  9. ?>
  10.  
Oct 9 '12 #5
Rabbit
12,516 Expert Mod 8TB
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.

Expand|Select|Wrap|Line Numbers
  1. $str = "Hello World";
  2. echo "$str";
  3.  
  4. Result:
  5. Hello World
Expand|Select|Wrap|Line Numbers
  1. $str = "Hello World";
  2. echo '$str';
  3.  
  4. Result:
  5. $str
Oct 9 '12 #6
@Rabbit
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.
Oct 9 '12 #7
Rabbit
12,516 Expert Mod 8TB
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.
Expand|Select|Wrap|Line Numbers
  1. echo "document.write(\"start<BR>" . $quotes[$ranNum] . "<BR>end\");";
Oct 9 '12 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: nickolausp | last post by:
When embedding a JavaScript document.write within a JavaScript document.write in Netscape 4.x (example below), the text is written out of logical order. <script type="text/javascript"> <!--...
2
by: Brett Baisley | last post by:
Hello I have a block of html code that I want to run by calling a javascript function to print it. Its basically a table with menu items in it that is the same for many pages, and instead of...
2
by: Geoff Wilkins | last post by:
I am using <SCRIPT src=...> to import a Javascript routine from a remote source. I am then using some of the variables given values in the routine, in my own Javascript.. Unfortunately the...
3
by: Gretjns | last post by:
I'm trying to figure out this script doesn't display any text in the child window and why I'm getting the null or not an object error. It's taken directly from the Javascript and DHTML cookbook...
12
by: Sean | last post by:
Hi, I have the following script: ----------------------------------------------------------------------------------- <script type="text/javaScript"> <!-- document.write('<div...
11
by: Michael Powe | last post by:
How can I make an XHTML-compliant form of an expression in this format: document.write("<scr"+"ipt type='text/javascript' src='path/to/file.js'>"+"</scr"+"ipt>"); this turns out to be a...
9
by: sonnystarks | last post by:
I am taking a course in writing javascript and it (and all the books I have been reading) tell me that if I will use the document.write syntax, I will be able to "place text on the page." None...
5
by: SuneR | last post by:
Hi, I am having problems figuring out how to make Firefox behave, and output the HTML I want it to output. The thing I want done is actually quite simple. I have a <labeltag, and inside it, I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.