Iimplement a price range slider using JavaScript
How to implement a price range slider using JavaScript's XMLHttpRequest Object.
The range slider can be useful for allowing users to select a specific price range when browsing products. Frequently you will see using this widget in several sites, especially in ecommerce websites. Today, I will show how to use this price slider with JavaScript and jQuery and PHP.
This example using mobile UI Two Point range sliders, where you will see start and end ranges in a single slider.
Here is a code example. There are two files, one is PHP file, which provides a backend code, it receives a range values and returns them back to frontend when user changes the range values.
Follow the steps given below
Code Example:
Step-1
create a PHP file with given name current_prices.php and copy the given code in it and save. It will receive max and min values from the form.
current_prices.php
The above PHP file get called dynamically in JavaScript function: updatePrices() using Ajax request object (XMLHttpRequest Object.)
Step-2
create a file and copy the below code and save it. Open in server and execute it. And check the output.
price-range-slider.php
<!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css">
<script src="https://code.jquery.com/jquery-1.11.3.min.js"></script> <script src="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
</head> <body> <div data-role="page"> <div data-role="header"> <h1>How implement a price range slider Using the XMLHttpRequest Object</h1> <p>The range slider can be useful for allowing users to select a specific price range when browsing products.</p> </div> <script type="text/javascript"> function updatePrices() { var min = document.getElementById("price-min").value; var max = document.getElementById("price-max").value; // alert(min); var url="current_prices.php?max="+max+"&min="+min; var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { document.getElementById("demo").innerHTML = this.responseText; } }; xhttp.open("GET", url, true); xhttp.send(); } </script> <div id="demo"> <h2>Change This</h2> </div> <div data-role="main" class="ui-content"> <div data-role="rangeslider"> <label for="price-min">Price:</label> <input type="range" name="price-min" id="price-min" value="200" min="0" max="1000"> <label for="price-max">Price:</label> <input type="range" name="price-max" id="price-max" value="800" min="0" max="1000"> </div> <button type="button" onClick="updatePrices();">Click Here!</button> </div> </div> </body> </html>
Result:


Comments
Post a Comment