doudansui6650 2013-04-10 14:37
浏览 30
已采纳

php代码中的JavaScript脚本标记

I have one page (parent) which opens a second page via popup (child)

On the second page I have the following PHP code which gets the value of an HTML element from the parent page:

 $var=print_r("<script type='text/javascript'>var x=window.opener.document.getElementsByName('name1');document.write(x[0].value)</script>",true);   

When I echo the variable $var I get exactly what I expect. Thus:

echo "test=" . $test;

... prints for example "Expenses" on the page.

So far so good.

The problem is when I try to write this variable to a file like:

$f=fopen("/mylog.txt","w+");
fwrite($f, $test);
fclose($f);

... then , instead of the actual value of $test (e.g. Expenses),

I get the whole script tag in my logfile, thus:

<script type='text/javascript'>var x=window.opener.document.getElementsByName('name1');document.write(x[0].value)</script>

Assuming that print_r with 'true' parameter returns the value to my $test variable, why is it writing the exact script tag to the logfile?

How can I overcome this?

  • 写回答

3条回答 默认 最新

  • dongxun7962 2013-04-10 14:40
    关注

    When you echo the value to a browser, it will run the JavaScript and display the result.

    When you save it to a file, the JavaScript isn't executed.

    In both cases, the full script is output, but the browser is actually running the script, whereas your text editor won't.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)