Reputation: 41
I have a little script that makes a multiple check on two hidden input fields:
function checkfs()
{
var rating1 = (document.getElementById("rating").value);
var rating2 = (document.getElementById("rating").value);
var rating3 = (document.getElementById("rating").value);
var check1 = (document.getElementById("countpl").value);
var check2 = (document.getElementById("countpl").value);
var check3 = (document.getElementById("countpl").value);
if (rating3 == 3 && check3 > 22 || check3 < 19){
alert("message 1");
window.location.href = 'myteam.php';}
else if (rating2 == 2 && check2 > 21 || check2 < 18){
alert("message 2");
window.location.href = 'myteam.php';}
else if (rating1 == 1 && check1 > 20 || check1 < 17){
alert("message 3");
window.location.href = 'myteam.php';}
else {return true;}
}
window.onload = checkfs;
HTML
<input name="countpl" id="countpl" type="hidden" value="<?php echo $row_checkfs['count(f_player.id)']; ?>"/>
<input name="rating" id="rating" type="hidden" value="<?php echo $row_checkfs['rating']; ?>"/>
I can’t understand how to visualize the correct alert depending on the type of control that has been made. At the moment I always see "alert( "message 1" )" whatever is the problem that has been found. I want the message 1 to appear if rating3 == 3 && check3 > 22 || check3 < 19, the message 2 to appear if rating2 == 2 && check2 > 21 || check2 < 18 etc. How do I modify the code in order to get this result?
Upvotes: 0
Views: 7464
Reputation: 1462
Also consider using functions for repetitive tasks,
function checkfs()
{
var rating = (document.getElementById("rating").value);
var check = (document.getElementById("countpl").value);
alert("rating="+rating+" - Check="+check);
if (rating == 3 && checkThis(check,19,22)){
alert("message 1");
window.location.href = 'myteam.php';}
else if (rating == 2 && checkThis(check,18,21)){
alert("message 2");
window.location.href = 'myteam.php';}
else if (rating == 1 && checkThis(check,17,20)){
alert("message 3");
window.location.href = 'myteam.php';}
else {return true;}
}
function checkThis(tocheck, min, max)
{
return tocheck<min || tocheck>max;
}
Upvotes: 1
Reputation: 2500
try
<script>
function checkfs()
{
var rating = (document.getElementById("rating").value);
var check = (document.getElementById("countpl").value);
if (rating == 3 && (check > 22 || check < 19)){
alert("message 1");
window.location.href = 'myteam.php';
}
else if (rating == 2 && (check > 21 || check < 18)){
alert("message 2");
window.location.href = 'myteam.php';
}
else if (rating == 1 && (check > 20 || check < 17)){
alert("message 3");
window.location.href = 'myteam.php';
}
else {return true;}
}
window.onload = checkfs;
</script>
Upvotes: 1
Reputation: 4659
Try this:
function checkfs()
{
var rating = (document.getElementById("rating").value);
var check = (document.getElementById("countpl").value);
alert("rating="+rating+" - Check="+check);
if (rating == 3 && (check > 22 || check < 19)){
alert("message 1");
window.location.href = 'myteam.php';}
else if (rating == 2 && (check > 21 || check < 18)){
alert("message 2");
window.location.href = 'myteam.php';}
else if (rating == 1 && (check > 20 || check < 17)){
alert("message 3");
window.location.href = 'myteam.php';}
else {return true;}
}
I added an alert to see the actual values.
I also used 2 variables instead of 6, and added the parenthesis on the "or" condition.
I think your main reason of failure was the parenthesis on the "or" condition.
You should review the theory about the precedence of the operators.
Upvotes: 3