Reputation: 939
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,
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! :)
Upvotes: 0
Views: 200
Reputation: 10148
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.
Upvotes: 0
Reputation: 244
I'm guessing there's not just one page where you want to put this logic. Here's what I'd do:
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'.
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.
Upvotes: 0
Reputation: 1170
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);
});
Upvotes: 0
Reputation: 1199
You can do this in 2 ways:
Anyway, PHP is not really needed
Upvotes: 1
Reputation: 4712
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.
Upvotes: 1
Reputation: 4947
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.
Upvotes: 1