Skip to content
Advertisement

Using PHP to validate range slider

I have a simple range slider that allows a user to select an age

<div>
 <label for="age">Age: </label><label id="ageOutput"></label><br><br>
 <input type="range" id="age" name="vol" min="0" max="99" value="16">
 <br>
 <span class="error">* <?php echo $ageErr;?></span>
</div>

And I use JavaScript update a label with the value of the slider

<script>
 var slider = document.getElementById("age");
 var output = document.getElementById("ageOutput");
 output.innerHTML = slider.value; // Display the default slider value

 slider.oninput = function() {
 output.innerHTML = this.value;
 }
</script>

I’m trying to use PHP to check the value of the slider and validate whether it’s between 16 and 90 but I keep getting an error that “age” doesn’t exist.

This is the PHP:

if (($_POST["age"]) < 16 || ($_POST["age"]) > 90)
    {
      $ageErr = "Customers must be aged 16-90";
    } 
    else 
    {
      $rangeSlider = test_input($_POST["ageOutput"]);
    }

Not really sure where to go from here because this method has been working for other values in the form.

Advertisement

Answer

You have to make change some portion in HTML Code.

<input type="range" id="age" name="vol" min="0" max="99" value="16">

hear you have to replace name="vol" to name="age". I think after this kind of change in code your code works fine.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement