Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/430.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将js变量作为php变量返回_Javascript_Php_Variables - Fatal编程技术网

Javascript 将js变量作为php变量返回

Javascript 将js变量作为php变量返回,javascript,php,variables,Javascript,Php,Variables,我喜欢将JS变量“latlng”传递给php变量,但它不起作用。在跨度内,它运行良好。代码末尾的php变量返回“[object HTMLSpanElement]”,而corret是我的当前位置 <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script> <script type="text/javascript"> var

我喜欢将JS变量“latlng”传递给php变量,但它不起作用。在跨度内,它运行良好。代码末尾的php变量返回“[object HTMLSpanElement]”,而corret是我的当前位置

<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
    var map;
    var geocoder;
    var centerChangedLast;
    var reverseGeocodedLast;
    var currentReverseGeocodeResponse;


    function initialize() {

        var myOptions = {
            zoom: 16,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
        geocoder = new google.maps.Geocoder();
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(function(position) {
                var latlng = new google.maps.LatLng(position.coords.latitude,
                    position.coords.longitude);
                map.setCenter(latlng);
            }, function() {
                handleNoGeolocation(true);
            });
        } else {
            // Browser doesn't support Geolocation
            handleNoGeolocation(false);
        }

        setupEvents();
        centerChanged();
    }

    function handleNoGeolocation(errorFlag) {
        if (errorFlag) {
            var content = 'Error: The Geolocation service failed.';
        } else {
            var content = 'Error: Your browser doesn\'t support geolocation.';
        }

        var options = {
            map: map,
            position: new google.maps.LatLng(60, 105),
            content: content
        };

        var infowindow = new google.maps.InfoWindow(options);
        map.setCenter(options.position);
    }

    function setupEvents() {
        reverseGeocodedLast = new Date();
        centerChangedLast = new Date();

        setInterval(function() {
            if ((new Date()).getSeconds() - centerChangedLast.getSeconds() > 1) {
                if (reverseGeocodedLast.getTime() < centerChangedLast.getTime())
                    reverseGeocode();
            }
        }, 1000);

        google.maps.event.addListener(map, 'zoom_changed', function() {
            document.getElementById("zoom_level").innerHTML = map.getZoom();
        });

        google.maps.event.addListener(map, 'center_changed', centerChanged);

        google.maps.event.addDomListener(document.getElementById('crosshair'), 'dblclick', function() {
            map.setZoom(map.getZoom() + 1);
        });

    }

    function getCenterLatLngText() {
        return '(' + map.getCenter().lat() + ', ' + map.getCenter().lng() + ')';
    }

    function centerChanged() {
        centerChangedLast = new Date();
        var latlng = getCenterLatLngText();
        document.getElementById('latlng').innerHTML = latlng;
        document.getElementById('formatedAddress').innerHTML = '';
        currentReverseGeocodeResponse = null;
    }

    function reverseGeocode() {
        reverseGeocodedLast = new Date();
        geocoder.geocode({
            latLng: map.getCenter()
        }, reverseGeocodeResult);
    }

    function reverseGeocodeResult(results, status) {
        currentReverseGeocodeResponse = results;
        if (status == 'OK') {
            if (results.length == 0) {
                document.getElementById('formatedAddress').innerHTML = 'None';
            } else {
                document.getElementById('formatedAddress').innerHTML = results[0].formatted_address;
            }
        } else {
            document.getElementById('formatedAddress').innerHTML = 'Error';
        }
    }


    function geocode() {
        var address = document.getElementById("address").value;
        geocoder.geocode({
            'address': address,
            'partialmatch': true
        }, geocodeResult);
    }

    function geocodeResult(results, status) {
        if (status == 'OK' && results.length > 0) {
            map.fitBounds(results[0].geometry.viewport);
        } else {
            alert("Geocode was not successful for the following reason: " + status);
        }
    }

    function addMarkerAtCenter() {
        var marker = new google.maps.Marker({
            position: map.getCenter(),
            map: map
        });

        var text = 'Lat/Lng: ' + getCenterLatLngText();
        if (currentReverseGeocodeResponse) {
            var addr = '';
            if (currentReverseGeocodeResponse.size == 0) {
                addr = 'None';
            } else {
                addr = currentReverseGeocodeResponse[0].formatted_address;
            }
            text = text + '<br>' + 'address: <br>' + addr;
        }

        var infowindow = new google.maps.InfoWindow({
            content: text
        });

        google.maps.event.addListener(marker, 'click', function() {
            infowindow.open(map, marker);
        });
    }
</script>


<body onLoad="initialize()">
    <h1 style="display:none;">APP Google Maps - Localizar Latitude / Longitude</h1>
    <div style="position: relative; width:980px; left:50%; margin-left:-490px; height:90px;">
        <div style="position:absolute; left:0px; top:20px;">
        </div>

        <div style="position:absolute; right:0px; top:30px;">
            Localizar Região:
            <input type="text" class="form_contato" id="address" style="width:300px; margin-right:15px;" />

            <input type="button" value="Procurar" onClick="geocode()" class="form_contato">
            <input type="button" value="Adicionar ponto no mapa" onClick="addMarkerAtCenter()" class="form_contato" />
        </div>
    </div>
    <div style="position: relative; width:980px; left:50%; margin-left:-490px; height:40px; line-height:40px; text-align:center;" class="Caecilia17">Para realizar a pesquisa digite no formulario acima o endereço desejado.</div>
    <div style="background:#ffffff;">
        <div id="map">
            <div id="map_canvas" style="width:100%; height:500px"></div>

            <div id="crosshair"></div>
        </div>
    </div>
    <div style="position: relative; width:980px; left:50%; margin-left:-490px; height:90px; margin-top:20px; line-height:20px;" class="arial15cinza">
        Latitude / Longitude: <span id="latlng"></span>
        <br />Endereço: <span id="formatedAddress"></span>
        <br />Nivel do zoom: <span id="zoom_level"></span>
        <br />
<?php
//escrevendo a variável JS pelo PHP
$latlng = echo "<script>document.write(latlng);</script>"
?>
    </div>
</body>

一种解决方案是通过查询字符串将javascript变量传递给PHP页面。 你可以这样做:

var newVar = "somevalue";
window.location.href = "http://stackoverflow.com?variable=" + newVar;
然后在php页面中抓取它,就像这样

$phpVariable = $_GET['variable'];

我希望这能有所帮助,同时谷歌服务器端与客户端编码也能帮助您更好地理解它们之间的交互方式

PHP是一种在您的服务器上运行的语言。它的工作是生成一个包含HTML/JavaScript/CSS的页面,并将其发送到浏览器。一旦该页面被发送到浏览器,PHP就完成了运行。然后由浏览器运行JavaScript并呈现HTML。如果要向PHP发送内容,需要通过POST、AJAX或其他方式向服务器发出新请求。echo没有返回值,因此执行$var=echo;对$var不执行任何操作,我认为这甚至可能是一个语法错误。P.P.S.不要使用document.write,有更好的方法将文本附加到页面中如果页面已完全加载,它将在写入之前删除整个页面。如果要将结果保存到数据库中,请使用ajax将变量发送到php文件。这是大量代码。请编辑您的问题,使其仅包含与您的问题相关的部分?请看一些提示。