can';t从php输出的html中执行javascript onclick事件

can';t从php输出的html中执行javascript onclick事件,javascript,php,html,Javascript,Php,Html,我被难住了…我试图在php块内执行纬度和经度搜索,但当按下按钮执行javascript时,它只在php之外执行…有解决办法吗 我的代码 <?php if(!isset($_SESSION['email'])): ?> <P class="lead">You are not authorized to view this page.</P> <?php else: ?> <form method=

我被难住了…我试图在php块内执行纬度和经度搜索,但当按下按钮执行javascript时,它只在php之外执行…有解决办法吗

我的代码

<?php if(!isset($_SESSION['email'])): ?>
    <P class="lead">You are not authorized to view this page.</P>
        <?php else: ?>
            <form method="post" action="">
                <div class="form-row">
                    <div class="form-group col-md-4">
                        <label class="form-check-label" for="latitude">Latitude</label>
                        <input type="text" name="latitude" class="form-control" id="latitude" value="<?php echo $latitude;?>">
                    </div>
                <div class="form-group col-md-4">
                    <label class="form-check-label" for="longitude">Longitude:</label>
                    <input type="text" name="longitude" class="form-control" id="longitude" value="<?php echo $longitude;?>">
                </div>

                <button onclick="getLocation()">Use Current Latitude & Longitude</button> 
                //this button does not execute here but it will execute if placed AFTER the <?php endif ?> found below...This is killing me...I'm thinking the function and button onclick event aren't communicating because of it's placement within the php code but is there a work around?...i tried escaping the quotes and double quotes to single and vice versa and replacing the blocks of code in different areas but couldn't get it to work
                </div>

            </form></div>
            <script type="text/javascript">
                var lat = document.getElementById("latitude");
                var long = document.getElementById("longitude");

                function getLocation() {
                    if (navigator.geolocation) {
                        navigator.geolocation.getCurrentPosition(showPosition);
                    } else {
                        lat.value = "Geolocation is not supported .";
                        lat.value = "Geolocation is not supported by this browser.";
                    }
                }

                function showPosition(position) {
                    lat.value = position.coords.latitude;
                    long.value = position.coords.longitude;
                }
            </script>
<?php endif ?>

您无权查看此页面。

纬度

//就像我上面所说的……如果我把这个按钮放在这个php结束之后,它是否会工作呢?

那么您需要使用session_start()初始化php会话。请参阅:。在if语句之前,应该调用session_start()

你说“它只在php之外执行”是什么意思?你知道javascript只在浏览器中运行,php只在服务器上运行,不是吗?因此,您不能在PHPAre中运行javascript
inline
,您希望javascript在服务器端执行吗?如果可能的话,它将返回您的服务器地理位置,而不是您的用户。您需要将值从(lat和long)传递给函数(getLocation).:函数getLocation(lat,long){……}和showPosition函数一样,会话启动了…我没有把所有的代码都放进去,但是我有一个包含会话的头。在结束表单标记之后有一个结束div标记。这似乎不合适