javascript 在同一页面上将 jQuery 变量传递给 PHP?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7680571/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Passing jQuery variable to PHP on the same page?
提问by Wordpressor
I know this is a frequent question on Stack Overflow but I couldn't find scenario where the PHP code is just above JS script in the same file. I guess ajax POST/GET won't do the job in this case?
我知道这是 Stack Overflow 上的一个常见问题,但我找不到 PHP 代码位于同一文件中的 JS 脚本上方的场景。我猜在这种情况下 ajax POST/GET 不会完成这项工作?
(...)
var ww = $(window).width();
</script>
<?php $ww = [data here] ?>
回答by tessr
You willhave to use POST/GET to send a JS variable to PHP, because PHP is processed by the server and JS is processed by the client.
您将不得不使用 POST/GET 将 JS 变量发送到 PHP,因为 PHP 由服务器处理而 JS 由客户端处理。
I know this isn't what you want to hear.
我知道这不是你想听到的。
回答by g.d.d.c
This is the wrong approach. The PHP is all parsed / evaluated server side. Later, the users' browser gets to parse / evaluate the javascript. Even if the JS appears before the PHP in your file, the PHP is still evaluated server side before anything is passed to the browser. If you need client side values passed back to the server, you use AJAX for that. You can make decisions on the server side again, and in the response, trigger the JavaScript to take some action based on the decision the PHP made, but you don't get to mix / match client / server actions in a single file like this.
这是错误的做法。PHP 都是经过解析/评估的服务器端。稍后,用户的浏览器开始解析/评估 javascript。即使 JS 出现在您的文件中的 PHP 之前,PHP 在任何内容传递给浏览器之前仍会在服务器端进行评估。如果您需要将客户端值传递回服务器,则可以使用 AJAX。您可以再次在服务器端做出决定,并在响应中触发 JavaScript 根据 PHP 做出的决定采取一些行动,但您不能像这样在单个文件中混合/匹配客户端/服务器操作.
回答by Steven
I don't think there is any way, because javascript runs on your local machine AFTERPHP has been executed and has returned HTML/CSS/JS to your browser.
我认为没有任何办法,因为在执行 PHP 并将 HTML/CSS/JS 返回到您的浏览器后,javascript 会在您的本地计算机上运行。
回答by Maarten Witteveen
What I did recently was passing a variable value to the attribute of an element that was dynamically generated by php. Then using the get attr() function of jquery to get the value. Worked perfectly!
我最近所做的是将一个变量值传递给由 php 动态生成的元素的属性。然后使用jquery的get attr()函数来获取值。完美地工作!
PHP part:
PHP部分:
foreach ((array)$photos['photos']['photo'] as $photo) {
$Maxcount++;
}
foreach ((array)$photos['photos']['photo'] as $photo) {
echo "<img imgID='$IDcount' Maxcount='$Maxcount'
jQuery:
jQuery:
var maxCount= $(".imageView").attr("maxCount");
回答by Watermark Studios
I would use AJAX to pass what I need to the server to be processed, then based on the response, using Javascript, make any necessary adjustments. For example:
我会使用 AJAX 将我需要的内容传递给服务器进行处理,然后根据响应,使用 Javascript 进行任何必要的调整。例如:
***PHP File***
<?php
$ww = $_POST['data'];
$result = doSomethingWithVariable($ww);
echo $result;
***HTML File***
<script>
...
var ww = $(window).width();
$.ajax({
url: 'phpfile.php',
type: 'POST',
data: { data : ww },
success: function (result) {
doSomethingWithResult(result);
}
});
</script>