【问题标题】:Pass value from jQuery into PHP script in ONE file将值从 jQuery 传递到 ONE 文件中的 PHP 脚本
【发布时间】:2015-09-27 21:28:00
【问题描述】:

以下是index.php 文件中GetWidth 和GetHeight 函数的作用。 (它们不是最终函数,所以不重要。)重要的部分由 /* IMPORTANT HERE */ 我想要完成的是传递我使用 jQuery Window Portal Height / Width 获得的变量并将其传递给 PHP 脚本here换句话说,当我从 GetWidth() 获取值时,它应该转到 w=120 或将 120 值替换为当前宽度和高度相同的值。如有语法错误请忽略。

我只想弄清楚如何将值从 jQuery 传递到 PHP 脚本。同样在将来我想添加 resize 方法,以便在调整窗口大小时值将是动态的。

<html>
    <head>
        <title></title>
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
        <script>
            $(window).resize(function () {
      $('#msg').text('Browser (Width : ' 
                         + $(window).width() 
         + ' , Height :' + $(window).height() + ' )');
        });

        $(document).ready(function() {
       // DOCUMENT READY

       function GetWidth() {
            if (self.innerWidth) {
                    return self.innerWidth;
                }
                else if (document.documentElement && 
                             document.documentElement.clientHeight) {
                    return document.documentElement.clientWidth;
                }
                else if (document.body) {
                    return 0;
                }
                return x;
            }

            function GetHeight() {
                if (self.innerHeight) {
                    y = self.innerHeight;
                }
                else if (document.documentElement && 
                           document.documentElement.clientHeight) {
                    return
                                document.documentElement.clientHeight;
                }
                else if (document.body) {
                    return 0;
                }
                return y;
            }





// This is for when it first loads.                                
$('#TEXT').text('W: ' + $(window).width() + ' , H:' + $(window).height() + y);

// This is for when window gets resized.
$(window).resize(function () {
    $('#TEXT').text('W: ' + $(window).width() + ' , H:' + $(window).height());
});

  // DOCUMENT READY    
  });
 </script>
 </head>

 <body>

Info Area:<br>
<div id="TEXT"></div>

/* IMPORTANT HERE */
<!-- img src="timthumb.php?src=URL...image.jpg&h=180&w=120" -->
/* IMPORTANT HERE */

</body>
</html>

【问题讨论】:

  • 这已被回答一百万次。尝试在右上角搜索或查看此问题右侧的“相关”部分。

标签: php jquery


【解决方案1】:

欢迎使用 stackoverflow!
问题是 php 正在服务器中编译,浏览器从服务器接收 html 和 javascript 文件
然后你的浏览器编译你的 javascript 和 html 代码
所以 javascript 无法更改 phpcode
您可以像这样使用 javascript 制作您的 html:

image = new Image(); 
image.src = "timthumb.php?src=URL...image.jpg&h="+ hgt + "&w=" + wth;

image.onLoad=function(){ $("img").attr({src: image.src}); }

【讨论】:

    【解决方案2】:

    您不能将 jQuery 或 JS 变量传递给 PHP 脚本。 基本上,PHP 脚本在服务器级别执行,只有 HTML、JS 和 CSS 将返回到浏览器。然后浏览器将呈现它。所以当浏览器执行你的 JS 方法时,PHP 脚本已经被执行了。

    你可以使用 Ajax。

    【讨论】:

      【解决方案3】:

      【讨论】:

        猜你喜欢
        • 2011-06-26
        • 2012-08-19
        • 2018-02-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多