1

I have a hidden field in a form where I'm trying to grab the users screen resolution. Then on the processing end retrieve the screen resolution via php. Unfortunately, my understanding of javascript is pretty limited. Here is what I have. I would really appreciate any help.

<head>
    <script type="text/javascript">
        function xy(){
            document.write(screen.width + "x" + screen.height);
            document.getElementById('xy').value;
            }           
    </script>
</head>




<form action="" method=post >

    //other fields here

    <input type="hidden" name="xy" id="xy" value=""/>
    <input type=submit name="button" value="button" /> 
</form>

When I view the page's source code, shouldn't I see the value set to for example "1366x768"? Now on the processing side, I would like to pull out information with php like this.

if(isset($_POST['xy']) && (!empty($_POST['xy'])){
 $blah = $_POST['xy'];
 //sanatize $blah;
    }

3 Answers 3

3

You can use

<script type="text/javascript">
    function setResolution()
    {
        document.getElementById('xy').value = screen.width + "x" + screen.height;
    }
</script>

Then, on submit, make sure the function is executed

<input type="submit" name="button" value="button" onclick="setResolution()" />
Sign up to request clarification or add additional context in comments.

2 Comments

thanks for your reply, onsubmit should do the same as onclick, right?
@ulliw That will take you less than a minute to test. ;-)
1

use

function xy(){
    document.getElementById('xy').value = screen.width + "x" + screen.height;

}

and use the script tag or execute the function after rendering of the form else the element would not be found

EDITED: added fiddle

2 Comments

Thanks for your fast reply and the useful fiddle. It makes a lot of sense, but still doesn't work for me.
hmm sorry no idea besides... fiddle works and is sending the data :(
1

ulliw,

you don't call the function so you don't get anything...

if you will change to this, i beleive it will work for you:

<head> 
<script type="text/javascript"> 
        document.write(screen.width + "x" + screen.height); //this will write on the html page 
        document.getElementById('xy').value=screen.width + "x" + screen.height; // this will put the resolution in your form's hidden field
</script> 

Comments

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.