简体   繁体   中英

Sending screen resolution via a form

I've got it sending the user agent already:

<input type='hidden' name='user-agent' value='<?php echo $_SERVER['HTTP_USER_AGENT']; ?>'/><input type="submit" value="Send User Agent" />

I've done my research and there is some code out there that sadly doesn't work. It's other people with problems asking for help themselves.

How can I go about sending screen resolutions using php forms?

You will have to fill in the screen resolution via JavaScript and then send it. This is not a safe method, since anyone who wanted to could change the value in the hidden field... But it will work:

// Fill in forms with screen resolution (jQuery)
$('#screenWidth').val(screen.width);
$('#screenHeight').val(screen.height);

And simply use a couple of hidden fields:

<input type="hidden" name="screenWidth" id="screenWidth"/>
<input type="hidden" name="screenHeight" id="screenHeight"/>

you will not get the screen resolution from the server IMHO... you could let the form be rendered and then use javascript to update a form input with the correct information

HTML

<input id="browser-resolution" type="hidden" name="resolution" value="">

and afterwards as a script

<script>
  document.getElementById('browser-resolution').value = screen.width + "x" + screen.height;
</script>

EDIT: added fiffle

The previous example should work:

Javascript

document.getElementById('debug').value = screen.width + 'x' + screen.height;

Are you getting any errors? Which browser are you using? OS?

As other people have said, the screen object works. Just saying "It doesn't work" doesn't help with finding the problem out. If you have jQuery correctly loaded, it will just work.

screen.width + "x" + screen.height

This returns "1280x1024" in my case.

See http://jsfiddle.net/KVQM4/ for an example of it working. If you can create something similar to show how it doesn't work, or give us a bit more information as to the error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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