2012-04-21 24 views
1

我有一個隱藏字段,我試圖抓取用戶屏幕分辨率。然後在處理結束通過php檢索屏幕分辨率。不幸的是,我對javascript的理解非常有限。這是我的。我真的很感激任何幫助。從窗體中檢索javascript輸出通過php

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

當我查看頁面的源代碼時,是否應該看不到設置爲例如「1366x768」的值?現在在處理方面,我想用這樣的php提取信息。

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

回答

3

你可以使用

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

然後,在提交時,請確保該函數執行

<input type="submit" name="button" value="button" onclick="setResolution()" /> 
+0

感謝您的回覆,onsubmit應該做的onclick一樣,對吧? – ulliw 2012-04-21 17:43:36

+0

@ulliw這將帶你不到一分鐘的時間來測試; - - ) – kba 2012-04-21 17:46:16

1

使用

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

} 

,並使用腳本標記或渲染其他元素不會被發現

編輯的形式後執行的功能:添加fiddle

+0

釷爲你的快速回復和有用的小提琴。它很有意義,但仍然不適合我。 – ulliw 2012-04-21 17:41:26

+0

嗯抱歉不知道除了...小提琴的作品,併發送數據:( – 2012-04-21 17:43:58

1

ulliw,

你不調用函數,所以你什麼也得不到......

,如果你將改變這一點,我相信這會爲你工作:

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

相關問題