views:

245

answers:

4

i have the following javascript code:

http://www.nomorepasting.com/getpaste.php?pasteid=22561

in which the function makewindows does not seem to be working.

it does actuall create a window, however the html either contains what is quotes, or if I change it to

child1.document.write(json_encode($row2["ARTICLE_DESC"]));

creats a blank html page.

I moved this function to my main javascript file to include because I was getting errors before, but now no html is presented in the popupwindow. Is this because I am not retrieving article_Desc in thest3.php?

The other 2 files used are here:

http://www.nomorepasting.com/getpaste.php?pasteid=22562

and test3.php

http://www.nomorepasting.com/getpaste.php?pasteid=22563

+1  A: 

$row2["ARTICLE_DESC"] is PHP variable.

Pawka
+3  A: 

to print php variable you need php tags:

child1.document.write(<?php echo json_encode($row2["ARTICLE_DESC"]); ?>);
Irmantas
That just gives an error, does it need quotes somewhere?
Joshxtothe4
what kind of error you get?
Irmantas
u need quotes here: document.write('<?php ?>');
Andreas Grech
A: 

I suspect that it is including the javascript after it has been parsed by the PHP interpreter. Try adding a parameter to makewindows and pass the value you intend to use in via the parameter when you construct the HTML.

 ...
 <p><a href='#' onclick='makewindows('"
  . json_encode($row2["ARTICLE_DESC"])
  . "'); return false;'>...


function makewindows(html){
   child1 = window.open ("about:blank");
   child1.document.write(html);
   child1.document.close();
}
tvanfosson
could you give an example? I tried passing $sql2 to makewindows but it did not like it.
Joshxtothe4
Is accepting a parameter preferred above the other examples?
Joshxtothe4
+4  A: 

$row2["ARTICLE_DESC"] is PHP variable.

It is indeed a php variable, but it is not being rendered as php because it is not enclosed in <?php ?> tags

So, the correct way to do it is:

child1.document.write(<?php echo json_encode($row2["ARTICLE_DESC"]); ?>);

That way, the php, being a server side language, will render the value in $row2 before the page is rendered, so when the page finally gets rendered, that value will be in the javascript write function...as it's supposed to be.

Andreas Grech
That gives an error without telling what the error is..missing quotes?
Joshxtothe4
json_encode is a php function, so it needs to be within the php tags
Tom Haigh
yes tomhaigh is right...corrected the error now.
Andreas Grech
Would it be more logical to allow the function to take a parameter, or to use the above method. What would be the respective advantages/disadvantages?
Joshxtothe4