简体   繁体   English

PHP传递Javascript收集的值

[英]PHP passing the values gathered by Javascript

Is it possible for a PHP script to pass the values gathered by Javascript? PHP脚本是否可以传递Javascript收集的值?

I'm just thinking if Javacript can capture the screen resolution of the user, can I use that value to run a PHP (IF and Else function) to load a certain php file? 我只是在考虑Javacript是否可以捕获用户的屏幕分辨率,我可以使用该值来运行PHP(IF和Else函数)来加载特定的php文件吗?

To have a clear explanation, 为了清楚说明,

  1. The Javascript will detect the user's screen resolution. Javascript将检测用户的屏幕分辨率。
  2. Once the screen resolution is determined, the PHP script will load the correct php file. 确定屏幕分辨率后,PHP脚本将加载正确的php文件。
  • IF screen resolution is GREATER THAN 1200, display PHP file No. 1 如果屏幕分辨率大于1200,则显示PHP文件1
  • IF screen resolution is GREATER THAN 1920, display PHP file No. 2 如果屏幕分辨率大于1920,则显示PHP文件2
  • IF screen resolution is LESS THAN 1200 = display PHP file No. 3 如果屏幕分辨率小于1200 =显示3号PHP文件

by the way, i forgot to mention that the PHP File is just a small portion of a webpage. 顺便说一句,我忘了提到PHP文件只是网页的一小部分。 Let's just assume that the php file is a header. 让我们假设php文件是一个头文件。 PHP file #1, #2 and #3 have different designs and google advertisements inside. PHP文件#1,#2和#3在内部具有不同的设计和Google广告。

PHP #1 & #2 has a 728px width google adsense ads while PHP #3 has a 300px width google adsense ads PHP#1&#2的Google Adsense广告宽度为728px,而PHP#3的Google Adsense广告宽度为300px

Is that possible? 那可能吗? your help is very much appreciated. 非常感激你的帮助。 Thank you very much! 非常感谢你! :) :)

You don't have to use PHP for it. 您不必为此使用PHP。

Maybe something like this?: 也许是这样的:

if ((screen.width>=1024) && (screen.height>=768))
{
 window.location="highres.html";
}
else
{
  window.location="lowres.html";
}

If you must use PHP, you can do a simple Ajax call. 如果必须使用PHP,则可以执行一个简单的Ajax调用。

Of course it's possible. 当然可以。 First you load the page which includes the script getting the resolution, then you send the values using a POST or a GET, and you return what you want. 首先,加载包含获取分辨率的脚本的页面,然后使用POST或GET发送值,然后返回所需的内容。

I'd suggest using some ajax, otherwise it could be disturbing for the user if the page load twice. 我建议使用一些ajax,否则如果页面加载两次,可能会对用户造成困扰。

You can do this in 2 ways: 您可以通过以下两种方式执行此操作:

  • Via javascript, decide the file to load according to the result 通过javascript,根据结果决定要加载的文件
  • Send the result to PHP (GET ot POST) and let it decides what to do (AJAX or redirect) 将结果发送到PHP(GET OT POST),然后决定要做什么(AJAX或重定向)

Anyway, PHP is not really needed 无论如何,实际上并不需要PHP

You can load a page with JQuery script and have PHP return the correct html file. 您可以使用JQuery脚本加载页面,并使PHP返回正确的html文件。 Then you call something like this: 然后,您可以这样调用:

$.post("page.php", {sel:"cond1", dpiy: dpi_y, dpix: dpi_x}, function(j)
{
    //load form
    $("#ext_page").html(j);
});

The other answers focus on AJAX which - for the sake of a few adsense ads - although cleaner, may be more complicated a solution than is really needed - for the sake of completeness, there is a cheap and easy alternative: dump everything to the page first and decide how to handle it after. 其他答案集中在AJAX上-为了提供一些adsense广告-尽管更干净,但解决方案可能比真正需要的解决方案更为复杂-为了完整起见,有一个便宜且容易的选择:将所有内容都转储到页面上首先,然后决定如何处理。

eg. 例如。 in your HTML: 在您的HTML中:

<div id="ADgt1200" style="display:none;">
  <!-- adsense code-->
</div>
<div id="ADgt1920" style="display:none;">
  <!-- adsense code-->
</div>
<div id="ADlt1200" style="display:none;">
  <!-- adsense code-->
</div>

then in your Javascript: 然后在您的Javascript中:

//pseudo
if(whatever.width > 1200)
  document.getElementById('ADgt1200').style.display = 'block';
else { /* etc... */ }

In response to your comment: 针对您的评论:

This was just a primitive example, on the same principle there is no reason why you couldn't store the adsense code in a javascript variable... 这只是一个原始示例,基于相同的原理,没有理由不能将adsense代码存储在javascript变量中...

<script>
    var myAdsense = [
      'gt1200': '<adsense/>',
      'gt1920': '<code/>',
      'lt1200': '<block/>'
    ];

    window.onload = function(){
      if(whatever.width > 1200)
        document.getElementById('myEl').innerHTML = myAdsense['gt1200'];
      // etc.
    };
</script>
<div id="myEl"></div>

... Thus, none of the code is actually loaded into the DOM unless you say so. ...因此,除非您这样说,否则没有任何代码实际加载到DOM中。

I'm guessing there's not just one page where you want to put this logic. 我猜想不只是一页要放置此逻辑。 Here's what I'd do: 这是我要做的:

  1. Check if screen resolution is available in $_SESSION['screen-res']. 检查$ _SESSION ['screen-res']中的屏幕分辨率是否可用。 If not, then redirect the browser to "check-screen-res.php". 如果不是,则将浏览器重定向到“ check-screen-res.php”。 This PHP file should have the relevant javascript code to detect screen resolution and then store it in session using the variable 'screen-res'. 该PHP文件应具有相关的javascript代码,以检测屏幕分辨率,然后使用变量“ screen-res”将其存储在会话中。

  2. If you have screen-res in your session, then you are good to go. 如果您的会话中有屏幕分辨率,那很好。 Simply use it to include whichever PHP file you want. 只需使用它即可包含所需的任何PHP文件。

Of course, this strategy won't work if the user changed his resolution during when he's on your site. 当然,如果用户在您访问网站时更改了分辨率,则此策略将行不通。 You could probably take care of that by raising appropriate event in case a screen resolution change is detected; 如果检测到屏幕分辨率变化,可以通过引发适当的事件来解决此问题。 And if so, update your session variable, and so forth, accordingly. 如果是这样,请相应地更新会话变量,依此类推。

Hope this helps. 希望这可以帮助。 Let me know if this is the strategy you want to follow, and I'll be happy to update this thread with some php/javascript code. 让我知道这是否是您要遵循的策略,我很乐意用一些php / javascript代码更新此线程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM