2012-02-15 3 views
1

내 숨겨진 필드 중 하나로 사용자 화면 크기를 가져 오려고합니다. 순수 HTML에서Drupal : 숨겨진 양식 구성 요소에 JavaScript 값을 추가하는 방법

, 나는 자바 스크립트로 입력 값, 예를 얻을 수 있습니다 :

document.write(screen.width + " x " + screen.height) 

내가 설정 한 경우 나, [전체 HTML] 페이지의 [입력 형식] 또는 [PHP 코드]를 설정 한 경우 의 구성 요소 값 -

<script>document.write(screen.width + " x " + screen.height);</script> 

는 SCRIPT 태그는 생략하고, 인용 표시는 "된다. [value] 필드가 안전한 HTML 엔터티로 변환 된 것 같습니다.

JavaScript 값이있는 숨겨진 폼 구성 요소를 Drupal 웹 폼에 추가하는 방법이 있습니까?

편집 : 해결 :

1 - Copy the original 
...sites/all/modules/webform/webform-form.tpl.php 
to 
...sites/all/themes/myTheme/webform-form-myFormNodeId.tpl.php 


2 - Add the following lines to webform-form-myFormNodeId.tpl.php 

    echo "<script>"; 
    echo "document.getElementById('edit-submitted-userscreen').value = screen.width + 'x' + screen.height;"; 
    echo "</script>"; 


3 - admin/settings/performance : [Clear cached data] 

편집 : 내가

%server[HTTP_USER_AGENT] 

어떻게 자바 스크립트 값을 추가하려면 일부 PHP 값을 추가 할 수 있습니까?

편집 2 : 나는 [PHP 코드] 다음 [양식 구성 요소] [값]에 노드를 [입력 형식]을 변경했지만, 그들 모두 작동하지 않았다 :

1 - drupal_add_js(screen.width + " x " + screen.height); 
2 - <script>document.write(screen.width + " x " + screen.height);</script> 
3 - <?php echo "<script language=\"javascript\">" . "document.write (screen.width + ' x ' + screen.height);" . "</script>"; ?> 

4 - full html + 
<script> 
jQuery('#edit-submitted-userscreen').val(screen.width + 'x' + screen.height); 
</script> 
=> in View Page Source, I see: 
<input type="hidden" name="submitted[userscreen]" id="edit-submitted-userscreen" value=" 
jQuery(&#039;#edit-submitted-userscreen&#039;).val(screen.width + &#039;x&#039; + screen.height); 


" /> 

을 [ SCRIPT 태그는 대체됩니다 NewLineChar ???]

=> 나는 양식을 작성 때, 나는 원래 문자열와 이메일 수신 :

userScreen: jQuery('#edit-submitted-userscreen').val(screen.width + 'x' + screen.height); 
+0

스크립트와 두 번째 변형 작업을해야합니다. –

+0

두 번째 변종, "소스보기"SCRIPT 태그가 생략되어 있으므로 내부 문자열을 얻을 수 있도록 [document.write (screen.width + "x"+ screen.height);] – Atara

+0

넣어보십시오 "Full HTML"입력 형식. 작동해야합니다. –

답변

2

그것은의를 간단하게 :

1) 양식에 숨겨진 필드를 정의하십시오.

$form['resolution'] = array(
    '#type' => 'hidden' 
); 

2) jQuery로 값을 필드에 적용하십시오.

$('#edit-resolution').val(screen.width + " x " + screen.height); 

편집을 할

시도가 활성화 Full HTML 모드와 텍스트 영역이 값을 제공합니다.

<input type="hidden" name="submitted[userscreen]" id="edit-submitted-userscreen" value="" /> 

<script> 
jQuery('#edit-submitted-userscreen').val(screen.width + 'x' + screen.height); 
</script> 

SECOND 편집

<input type="hidden" name="submitted[userscreen]" id="edit-submitted-userscreen" value="" /> 
<script>document.getElementById('edit-submitted-userscreen').value = screen.width + 'x' + screen.height;</script> 
+0

GUI를 사용하여 폼 구성 요소를 추가합니다. [값] 필드가 있어야합니다. – Atara

+0

죄송합니다. ] 버튼을 클릭하십시오. 제게 제공 한 코드는 어디에 써야합니까? –

+0

지금까지 [form component] [edit]을 사용하여 양식을 만들었습니다.이 양식 구성 요소는 무엇입니까? [$ val (screen.width + "x"+ screen.height)] – Atara

관련 문제