0

Is it possible for a PHP script to pass the values gathered by 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?

To have a clear explanation,

  1. The Javascript will detect the user's screen resolution.
  2. Once the screen resolution is determined, the PHP script will load the correct php file.
  • IF screen resolution is GREATER THAN 1200, display PHP file No. 1
  • IF screen resolution is GREATER THAN 1920, display PHP file No. 2
  • IF screen resolution is LESS THAN 1200 = display PHP file No. 3

by the way, i forgot to mention that the PHP File is just a small portion of a webpage. Let's just assume that the php file is a header. PHP file #1, #2 and #3 have different designs and google advertisements inside.

PHP #1 & #2 has a 728px width google adsense ads while PHP #3 has a 300px width google adsense ads

Is that possible? your help is very much appreciated. Thank you very much! :)

4
  • 2
    You could use AJAX. Once the JavaScript has captured the screen resolution, send a request to a page that will generate the readjusted output. Commented Mar 19, 2013 at 15:32
  • 1
    It's possible but as a rule of thumb perhaps UX/front-end design should be left to client-side languages. Commented Mar 19, 2013 at 15:33
  • 1
    Why not use CSS media queries and keep it all client side? I'm assuming you're doing this for layout purposes. Commented Mar 19, 2013 at 15:35
  • media queries is not an option. I've already look at that but unfortunately, it doesnt address the problem. Commented Mar 19, 2013 at 15:38

6 Answers 6

1

You don't have to use PHP for it.

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

I'd suggest using some ajax, otherwise it could be disturbing for the user if the page load twice.

Comments

1

You can do this in 2 ways:

  • Via javascript, decide the file to load according to the result
  • Send the result to PHP (GET ot POST) and let it decides what to do (AJAX or redirect)

Anyway, PHP is not really needed

Comments

0

You can load a page with JQuery script and have PHP return the correct html file. 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);
});

Comments

0

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']. If not, then redirect the browser to "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'.

  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.

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.

Comments

0

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.

eg. in your 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:

//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...

<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.

3 Comments

sorry but unfortunately, this will not work. Adsense allows maximum 3 ads per page. If I do that, the page will automatically load 3 adsense ads. Only 1 is visible to the user and the other two is hidden. my page is carrying 3 google ads, all of them is generating considerable revenue.
@KareenLagasca There's ways and means... It's definitely viable. Updated Answer.
Don't get me wrong, AJAX is awesome but it isn't absolutely necessary - I haven't looked at Adsense code in ages but surely it's in some kind of consistent format where you could even build the correct code on the fly?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.