dongzouxigu12345 2017-12-26 18:05
浏览 36

如何写回复文件?

The output of this script is fine in browser. I tested on myself and got accurate results. What I want to know is, how do I write the response to a file? I tried writing the response and all I got was the codes written instead of the output.

<?php

$loc = '<body onload="getLocation()">
<p id="demo"></p>

<script>
var x = document.getElementById("demo");
function getLocation() {
if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(showPosition);
} else { 
    x.innerHTML = "Geolocation is not supported by this browser.";
}
}
function showPosition(position) {
x.innerHTML = "Latitude: " + position.coords.latitude + 
"<br>Longitude: " + position.coords.longitude;
}
</script>';

//File Write
echo $loc;
$log = fopen("loc.txt", "a");
$info = "$loc
";
fwrite($log, $info);
fclose($log);

?>

Output = GPS Coordinates of the browser

  • 写回答

1条回答 默认 最新

  • doutu4335 2017-12-26 19:43
    关注

    You could try something along these lines - untested so there may be errors! I would suggest that you do not echo the body tag as you are like this ...

    <?php
    
        if( $_SERVER['REQUEST_METHOD']=='POST' && isset( $_POST['lat'], $_POST['lng'] ) ){
            ob_clean();
            $bytes = file_put_contents('loc.txt',json_encode( $_POST ) . PHP_EOL, FILE_APPEND );
            exit( $bytes ? 'ok' : 'bad foo' );
        }
    
    
    
    
        $loc = '<body onload="getLocation()"><p id="demo"></p>
        <script>
    
            var x = document.getElementById("demo");
    
            function ajax(url,callback,payload){
                var xhr=new XMLHttpRequest();
                xhr.onreadystatechange=function(){
                    if( this.status==200 && this.readyState==4 )callback.call(this,this.response)
                }
                xhr.open("POST",url,true);
                xhr.send( payload );
            }
            function getLocation() {
                if (navigator.geolocation) {
                    navigator.geolocation.getCurrentPosition(showPosition);
                } else { 
                    x.innerHTML = "Geolocation is not supported by this browser.";
                }
            }
    
            function showPosition( position ) {
                x.innerHTML = "Latitude: " + position.coords.latitude + "<br>Longitude: " + position.coords.longitude;
                ajax.call( this, location.href, function(r){
                    alert(r)
                }, 'lat='+position.coords.latitude+'&lng=' + position.coords.longitude );
            }
        </script>';
    
        echo $loc;
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line