Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/289.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和PHP接收浏览器解析_Javascript_Php - Fatal编程技术网

使用JavaScript和PHP接收浏览器解析

使用JavaScript和PHP接收浏览器解析,javascript,php,Javascript,Php,我需要一个按钮,当用户单击Submit按钮时,该按钮将转到下一个名为步骤的页面,并在该页面中返回用户的分辨率。我试着用下面的方法,但不起作用 <?php echo ' <script> function getSize() { var w = window.outerWidth; var h = window.outerHeight; document.getElementB

我需要一个按钮,当用户单击Submit按钮时,该按钮将转到下一个名为
步骤的页面,并在该页面中返回用户的分辨率。我试着用下面的方法,但不起作用

<?php
    echo '
    <script>
        function getSize() {
            var w = window.outerWidth;
            var h = window.outerHeight;
            document.getElementById("size").innerHTML = "" + w + "x" + h + "px";
        }
    </script>';

    if (isset($_POST['step']) && $_POST['step'] == 'docreate') {
        // should receive the user resolution - I was trying to do with <p id="size"></p> but isn't working.
    } else {
        echo '
        <form class="form-horizontal" role="form" action="" method="post">
            <div class="text-center">
                <button type="submit" class="btn btn-primary" name="step" value="docreate" onclick="getSize()">Submit</button>
                <button type="reset" class="btn btn-default">Cancel</button>
            </div>
        </form>';
    }

您的代码正在运行。由于您使用的是提交操作,因此脚本将运行,然后页面将重新加载,从而丢失以前的脚本结果

你可以用

<script>
   window.onload = function() {
      var w = window.outerWidth;
      var h = window.outerHeight;
      document.getElementById("size").innerHTML = "" + w + "x" + h + "px"; 
      //--- save the values to the FORM
      document.getElementById('width).value = w;
      document.getElementById('height').value = h;
}
</script>

window.onload=函数(){
var w=窗宽、外径;
var h=窗口。外部灯光;
document.getElementById(“大小”).innerHTML=”“+w+“x”+h+“px”;
//---将值保存到表单中
document.getElementById('width.).value=w;
document.getElementById('height')。值=h;
}
扩展答案以包含表单元素

<form>
  <input type="hidden" id="width" name="width" value="">
  <input type="hidden" id="height" name="height" value="">
  <button type="submit" class="btn btn-primary" name="step" value="docreate">Submit</button>
  <button type="reset" class="btn btn-default">Cancel</button>
</form>

提交
取消

对于整个屏幕分辨率,您可以使用

height = screen.height;
width = screen.width;
如果希望分辨率排除任务栏,请使用

height = screen.availHeight;
width = screen.availWidth;

为什么不在下一页检测分辨率?为什么要从另一个页面发布它?您是否正在尝试获取正文的宽度和高度?文档<代码>
,如果你尝试使用rosulation,你会发现它与你的代码一起工作,但现在我面临一个问题,我无法将值发送到数据库,值显示为

尝试这样使用,我看到你编辑了你的文章,我也更新了它,但是我还没有收到宽度和高度的值,尝试在您的示例中这样使用,您已经在Submit上删除了表单,但是它没有被执行,因为Widt和height的值没有显示。当我尝试使用像
$\u POST['width'].'x.$\u POST['height'.'px'这样的东西时,它返回为
xpx