admin管理员组文章数量:1026989
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 overe this?
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 overe this?
Share Improve this question edited Apr 10, 2013 at 14:45 Trinimon 14k9 gold badges46 silver badges61 bronze badges asked Apr 10, 2013 at 14:37 PortisheadPortishead 653 silver badges10 bronze badges 2- 3 that's because the javascript is interpreted by the browser. – Prisoner Commented Apr 10, 2013 at 14:38
-
1
Why on earth would you
$var = print_r('string', true);
a string? That acheives precisely nothing over$var = 'string';
– DaveRandom Commented Apr 10, 2013 at 14:41
3 Answers
Reset to default 4When 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.
Send your data which is on the client to the server. You can use Ajax (shown below) or a form.
$.post('myPHPfile.php',{name:window.opener.document.getElementsByName('name1')});
myPHPfile.php
$test=$_POST['name'];
$f=fopen("/mylog.txt","w+");
fwrite($f, $test);
fclose($f);
OK , I acplished the desired result by altering the url-string which calls the 2nd page with an extra variable (the desired one) and then , via $_GET , I retrieve this value and can print it without problems to my logfile.
Many thanks guys to all of you for the quick responses :)
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 overe this?
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 overe this?
Share Improve this question edited Apr 10, 2013 at 14:45 Trinimon 14k9 gold badges46 silver badges61 bronze badges asked Apr 10, 2013 at 14:37 PortisheadPortishead 653 silver badges10 bronze badges 2- 3 that's because the javascript is interpreted by the browser. – Prisoner Commented Apr 10, 2013 at 14:38
-
1
Why on earth would you
$var = print_r('string', true);
a string? That acheives precisely nothing over$var = 'string';
– DaveRandom Commented Apr 10, 2013 at 14:41
3 Answers
Reset to default 4When 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.
Send your data which is on the client to the server. You can use Ajax (shown below) or a form.
$.post('myPHPfile.php',{name:window.opener.document.getElementsByName('name1')});
myPHPfile.php
$test=$_POST['name'];
$f=fopen("/mylog.txt","w+");
fwrite($f, $test);
fclose($f);
OK , I acplished the desired result by altering the url-string which calls the 2nd page with an extra variable (the desired one) and then , via $_GET , I retrieve this value and can print it without problems to my logfile.
Many thanks guys to all of you for the quick responses :)
本文标签: JavaScript script tag inside php codeStack Overflow
版权声明:本文标题:JavaScript script tag inside php code - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745631505a2160193.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论