2016-11-12 9 views
0

웹 페이지에 현재 질문 번호를 표시하는 HTML TextBox가 있습니다. (어떤 질문에 대답 할 수있는 작은 웹 페이지), 사용자가 원하는 질문에 대한 바로 가기를 만들고 싶습니다. TextBox의 질문. 아래 코드를 사용하지만 올바르게 작동하지 않습니다. 모든 질문은 8이고 TextBox에 15를 입력하고 Enter 키를 누르면 if 절이 작동하지 않고 Question 변수가 15로 설정됩니다. 경고 기능을 사용하여 추적하고 if 절이 올바르게 작동하지 않는다는 것을 이해합니다. . 누군가 그것을 확인하고 나를 인도 할 수 있습니까? 이 내 모든 코드는 다음과 같습니다자바 스크립트 onkeypress에서 계산하기

<?php 
$All = 8; 
$URL = "http://localhost/Test.php"; 
if(isset($_GET["edtQuestionNo"])){ 
    $QuestionNo = $_GET["edtQuestionNo"]; 
}else{ 
    $QuestionNo = 1; 
} 
?> 
<html> 
<head> 
<title>Test Page</title> 
<script type="text/javascript"> 
function KeyPress(e, URL, All){ 
    if(e.keyCode === 13){ 
     var Question = document.getElementsByName("edtQuestionNo")[0].value; 
     if(Question > All){ 
      Question = All; 
      alert(All + " " + Question + " yes"); 
     } 
     else{ 
      alert(All + " " + Question + " no"); 
     } 
     window.open(URL + "?edtQuestionNo=" + Question,"_self"); 
    } 
} 
</script> 
</head> 
<body> 
    <form action="Test.php" method="get" name="FRMQuestion"> 
     <label>Enter question number : </label> 
     <input type="text" name="edtQuestionNo" id="QuestionNo" value="<?php echo $QuestionNo; ?>" 
      onkeypress="KeyPress(event,'<?php echo $URL; ?>','<?php echo $All; ?>')"> 
     <br> 
     <label>Question number is : <?php echo $QuestionNo; ?></label> 
    </form> 
</body> 
</html> 
+0

쇼는 "모든", 또한 당신의 HTML 컨테이너는 당신이 – repzero

+0

그냥 머리를이 키 누르기 이벤트를 추가 -이 코드 당신으로 XSS에 취약 : 내 코드입니다 인쇄하기 전에 사용자 입력 ($ QuestionNo)을 이스케이프 처리하지 않습니다. http://stackoverflow.com/questions/15755323/what-is-cross-site-scripting 및 http://stackoverflow.com/search?tab=votes&q=xss를 확인하십시오. – Dogbert

답변

0

가 나는 1. 나는 모든 및 질문 값을 비교하는 parseInt 기능을 사용할 수 있습니다 해결. 그들은 다른 유형이기 때문에. 2. HTML TextBox에 Question 값 (계산 후)을 입력 한 다음 URL을 엽니 다. "URL"변수에 어떤

<?php 
$All = 8; 
$URL = "http://localhost/Test.php"; 
if(isset($_GET["edtQuestionNo"])){ 
    $QuestionNo = $_GET["edtQuestionNo"]; 
}else{ 
    $QuestionNo = 1; 
} 
?> 
<html> 
<head> 
<title>Test Page</title> 
<script type="text/javascript"> 
function KeyPress(e, URL, All){ 
    if(e.keyCode === 13){ 
     var Question = document.getElementsByName("edtQuestionNo")[0].value; 
     if(parseInt(Question) > parseInt(All)){ 
      Question = All; 
     } 
     document.getElementsByName("edtQuestionNo")[0].value = Question; 
     window.open(URL + "?edtQuestionNo=" + Question,"_self"); 
    } 
} 
</script> 
</head> 
<body> 
    <form action="Test.php" method="get" name="FRMQuestion"> 
     <label>Enter question number : </label> 
     <input type="text" name="edtQuestionNo" id="QuestionNo" value="<?php echo $QuestionNo; ?>" 
      onkeypress="KeyPress(event,'<?php echo $URL; ?>','<?php echo $All; ?>')"> 
     <br> 
     <label>Question number is : <?php echo $QuestionNo; ?></label> 
    </form> 
</body> 
</html>