I have a HTML TextBox on my web page that show number of current question (A small web page to answer some question), I want to make shortcut to a question that users want by type number of question in TextBox. I use this code below but this not work correctly.
For example all questions are 8 and when I enter 15 in TextBox and press Enter, if clause don't work and Question
variable set with 15.
I use alert function to trace it and I understand that if clause don't work correctly. Can somebody check it and guide me?
This is all of my code:
<?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>
I solve it
1. I have to use parseInt
function for comparing All and Question value. because they are in different type.
2. I put Question value (after computing) in HTML TextBox again and then open URL.
My code is:
<?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>