Skip to main content

How to Create a Price Range Slider Filter Using PHP and Ajax

Screenshot
Body

In this tutorial we will create a Price Range Slider Using Ajax. PHP is a server-side scripting language designed primarily for web development. Using Ajax, data could then be passed between the browser and the server, using the XMLHttpRequest API, without having to reload the web page. It is designed to simplify the traditional way of coding in javascript.

So let's now do the coding...

Before we get started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html.

And this is the link for the jquery that i used in this tutorial https://jquery.com/.

Lastly, this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

Creating Database

Open your database web server then create a database name in it db_price. After that, click Import then locate the database file inside the folder of the application then click ok.

tut1

You can also create the product table progrmmatically. To do that, copy the code below paste it into provided Text Field in your PHPMyAdmin SQL Tab of your newly created database.

  1. CREATE TABLE `product` (
  2. `product_id` int(11) NOT NULL,
  3. `product_name` varchar(50) NOT NULL,
  4. `product_price` int(20) NOT NULL,
  5. `product_image` varchar(100) NOT NULL
  6.  
  7. ALTER TABLE `product`
  8. ADD PRIMARY KEY (`product_id`);
  9.  
  10. ALTER TABLE `product`

Creating the database connection

Open your any kind of text editor(notepad++, etc..). Then just copy/paste the code below then name it conn.php.

  1. <?php
  2. $conn = new mysqli("localhost", "root", "", "db_price");
  3.  
  4. if(!$conn){
  5. die("Error: Can't connect to database!");
  6. }
  7. ?>

Creating The Interface

This is where we will create a simple form for our application. To create the forms simply copy and paste it into your text editor, then save it as index.php.

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6. </head>
  7. <body>
  8. <nav class="navbar navbar-default">
  9. <div class="container-fluid">
  10. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  11. </div>
  12. </nav>
  13.  
  14. <div class="col-md-3"></div>
  15. <div class="col-md-6 well">
  16. <h3 class="text-primary">PHP - Price Range Slider Using Ajax</h3>
  17. <hr style="border-top:1px dotted #ccc;"/>
  18. <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#form_modal"><span class="glyphicon glyphicon-plus"></span> Add Product</button>
  19. <hr style="border-top:5px solid #000;"/>
  20. <br /><br />
  21.  
  22. <div id="product_list">
  23. <?php
  24. require 'conn.php';
  25.  
  26. $query = $conn->query("SELECT * FROM `product` ORDER BY `product_price` DESC");
  27. while($fetch = $query->fetch_array()){
  28. ?>
  29. <div class="pull-left" style="height:250px; width:200px;">
  30. <center><img src="<?php echo $fetch['product_image']?>" height="150px" width="180px"/></center>
  31. <center><h4><strong><?php echo $fetch['product_name']?></strong></h4></center>
  32. <center><h4><?php echo $fetch['product_price']?></h4></center>
  33. </div>
  34. <?php
  35. }
  36. ?>
  37. </div>
  38. <br />
  39. <input type="range" min="5000" max="60000" step="1000" value="10000" id="price"/>
  40. <center><span id="price_range">Products Below Price 10000</span></center>
  41. </div>
  42. <div class="modal fade" id="form_modal" tabindex="-1" role="dialog" aria-hidden="true">
  43. <div class="modal-dialog" role="document">
  44. <form action="save_query.php" method="POST" enctype="multipart/form-data">
  45. <div class="modal-content">
  46. <div class="modal-body">
  47. <div class="col-md-2"></div>
  48. <div class="col-md-8">
  49. <div class="form-group">
  50. <label>Product Name</label>
  51. <input class="form-control" type="text" name="product_name">
  52. </div>
  53. <div class="form-group">
  54. <label>Product Price</label>
  55. <input class="form-control" type="number" name="product_price">
  56. </div>
  57. <div class="form-group">
  58. <label>Product Photo</label>
  59. <input class="form-control" type="file" name="product_image">
  60. </div>
  61. </div>
  62. </div>
  63. <div style="clear:both;"></div>
  64. <div class="modal-footer">
  65. <button type="button" class="btn btn-danger" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Close</button>
  66. <button name="save" class="btn btn-primary"><span class="glyphicon glyphicon-save"></span> Save</button>
  67. </div>
  68. </div>
  69. </form>
  70. </div>
  71. </div>
  72. </body>
  73. <script src="js/jquery-3.2.1.min.js"></script>
  74. <script src="js/bootstrap.js"></script>
  75. <script src="js/script.js"></script>
  76. </html>

Creating PHP Query

This code contains the PHP query of the application. This code is consists of two different functionalities. First, this code can send the data inputs to the database, and it will display the data to the HTML by ajax request To do that just copy and paste this block of codes inside the text editor, then save it as shown below.

save_query.php
  1. <?php
  2. require_once 'conn.php';
  3.  
  4. if(ISSET($_POST['save'])){
  5. if(!empty($_POST['product_name']) && !empty($_POST['product_price'])){
  6. $product_name = $_POST['product_name'];
  7. $product_price = $_POST['product_price'];
  8.  
  9. $file = explode(".", $_FILES['product_image']['name']);
  10. $ext = array("png", "gif", "jpg", "jpeg");
  11.  
  12. if(in_array($file[1], $ext)){
  13. $file_name = $file[0].'.'.$file[1];
  14. $tmp_file = $_FILES['product_image']['tmp_name'];
  15. if(!is_dir('./upload'))
  16. mkdir('./upload');
  17. $location = "upload/".$file_name;
  18. $new_location = addslashes($location);
  19.  
  20. if(move_uploaded_file($tmp_file, $location)){
  21. $conn->query("INSERT INTO `product` VALUES('', '$product_name', '$product_price', '$new_location')");
  22. echo "<script>alert('Data Insert')</script>";
  23. echo "<script>window.location = 'index.php'</script>";
  24. }
  25.  
  26. }else{
  27. echo "<script>alert('File not available')</script>";
  28. echo "<script>window.location = 'index.php'</script>";
  29. }
  30.  
  31. }else{
  32. echo "<script>alert('Please complete the required field!')</script>";
  33. }
  34.  
  35.  
  36. }
  37. ?>
data.php
  1. <?php
  2. require_once 'conn.php';
  3.  
  4. $query = $conn->query("SELECT * FROM `product` WHERE `product_price` <= ".$_POST['price']." ORDER BY `product_price` DESC");
  5. $rows = $query->num_rows;
  6. if($rows > 0){
  7. while($fetch = $query->fetch_array()){
  8. ?>
  9. <div class="pull-left" style="height:250px; width:200px;">
  10. <center><img src="<?php echo $fetch['product_image']?>" height="150px" width="180px"/></center>
  11. <center><h4><strong><?php echo $fetch['product_name']?></strong></h4></center>
  12. <center><h4><?php echo $fetch['product_price']?></h4></center>
  13. </div>
  14. <?php
  15.  
  16.  
  17. }
  18.  
  19. }else{
  20. echo "<center><h3>No Result Found!</h3></center>";
  21. }
  22. ?>

Creating Ajax Script

This is where the code that uses ajax request been used. This code will send an ajax request to database server, to fetch the data and display within it.To do this just copy and paste these block of codes inside the text editor, then save it as script.js inside the js folder.

  1. $(document).ready(function(){
  2. $('#price').change(function(){
  3. var price = $(this).val();
  4. $("#price_range").text("Products Below Price " + price);
  5.  
  6. $.ajax({
  7. url: 'data.php',
  8. type: 'POST',
  9. data: {price: price},
  10. success: function(data){
  11. $("#product_list").html(data);
  12. }
  13. });
  14.  
  15. });
  16.  
  17. });

There you have it we successfully created Price Range Slider Using Ajax. I hope that this simple tutorial helps you to what you are looking for. For more updates and tutorials just kindly visit this site.

Enjoy Coding!!!

Add new comment