javascript - 如何将javascript变量发送到php mysql

标签 javascript php jquery html mysql

<分区>

我正在尝试将 javascript 变量插入到 php mysql 中,但它没有插入。它插入为 <javascript>document.write(window.outerWidth); </javascript> x <javascript>document.write(window.outerHeight); </javascript> .但结果是 1366 x 728

我该怎么办?

<?php
$width = " <script>document.write(window.outerWidth); </script>";
$height = " <script>document.write(window.outerHeight); </script>";
$xex = " x ";
$resulteee = "$width $xex $height";
echo $resulteee;
?>

最佳答案

AJAX 可以很好地解决您的问题:

<script type="text/javascript">
     function call_ajax () {
           var width = window.outerWidth;
           var height = window.outerHeight;

           var xmlhttp = new XMLHttpRequest();

           xmlhttp.onreadystatechange = function() {
                 if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                     document.getElementById("abc").innerHTML = xmlhttp.responseText;
                 }
             }
             xmlhttp.open("POST", "a.php?height="+height+"width="+width, true);
             xmlhttp.send();
        }
    </script>

在 a.php 页面上,您可以回显您的变量以获得如下输出:

<?php
echo $_POST['height'];
echo $_POST['width'];
die;

关于javascript - 如何将javascript变量发送到php mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32152818/

相关文章:

javascript - 尝试在discord.js 中记录消息

php - 将元素添加到 Ajax 响应返回的表单中

javascript - 向 FullCalendar 日添加更多字段/数据

php - Magento 事件观察器或自定义模块在生产中不起作用

jquery - 如何使用 Jquery(或 CSS5)选择直到 "this"

javascript - ScrollTop 仅在 IE Edge 中不起作用

javascript - 使用 jQuery 转换 DateTime URL 参数

php - 在XPath评估之前,如何处理字符串中的双引号?

javascript - 如何相邻打印 Fpdf MultiCell

javascript - 超时在ajax post请求中不起作用