2016-11-12 51 views
0

我有我的網頁上的HTML文本框,顯示當前問題的數量(一個小網頁來回答一些問題),我想使快捷方式到一個問題,用戶想要的型號問題在TextBox中。 我在下面使用這段代碼,但是這不正確。 敵人例如,所有的問題都是8,當我在進入15 TextBox和回車,如果條款不工作,Question變量集與15 我使用警報功能來跟蹤它,我明白,如果條款無法正常工作。有人可以檢查它並引導我嗎? 這是我的所有代碼:做在JavaScript的一些計算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

顯示什麼是變量「URL」,「全部」,也是你的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文本框中輸入問題值(計算後),然後打開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>