I am using the jQuery slider component in one of my forms as follows:
<form action="survey_dis.php" method="POST" target="_self" name="apForm">
<div data-role="rangeslider">
<input type="range" name="F1" id="F1" min="0" max="100" step="5" value="" onchange='document.getElementById("bar1").value = document.getElementById("F1").value."%";'>
<input type="text" name="bar1" id="bar1" value="" size="3" disabled />
</div>
(...)
</form>
As you note, the allowed range should indicate percent, so the allowed values have to be between 0 and 100. Because a check of empty($var) will return TRUE even for "0", I've defaulted "value" with an empty string...
Now in my php script, I need to check whether the user has actually selected a value instead of keeping it at the default:
for ($i=1; $i<51; $i++){ // 51 questions
if (isset($_POST["F$i"]) && strlen($_POST["F$i"]) && ($_POST["F$i"]<>'-1') ){
$F_var = $_POST["F$i"];
} else {
print "Question # $i missing";
$missing_fields += 1;
}
}
However, it always looks to php as if a slider selection has been made and the "else" condition never applies... even if I default "value" with "0".
First of all you have a syntax error in your onchange
. Your %
is concatenated with a .
instead of a +
document.getElementById("F1").value + "%";'
Why does the value 0 return true?
You check the string length of it, which is 1, even for a 0
. The other two conditions are of course also true, since 0 != -1
and the given parameter is set.
You could send bar
fields instead and check them as they are on default empty. Note: to do this the field cannot be disabled
as it would be never send in this case. You could set it to readonly
for example.
Hint: You start your for loop at $i=1
. So your last question will never be checked, as you stop looping on $i<51
instead of $i<=51