Creating a Live Search in Table using Vue.js and PHP

This tutorial tackles on how to create a search function that searches MySQL table using Vue.js with the help of axios.js and PHP. In this tutorial, you can search for either the first name or the last name of members in the members' table then it will return members that match your search.

Getting Started

I've used CDN for Bootstrap, Vue.js, and Axios.js so, you need an internet connection for them to work. And also download XAMPP

Creating our Database

  • Open your XAMPP's Control Panel and start "Apache" and "MySQL"
  • Open PHPMyAdmin
  • Click databases, create a database and name it as vuetot
  • After creating a database, click the SQL and paste the below codes. See the image below for detailed instructions.
  1. CREATE TABLE `members` (
  2. `memid` int(11) NOT NULL AUTO_INCREMENT,
  3. `firstname` varchar(30) NOT NULL,
  4. `lastname` varchar(30) NOT NULL,
  5. PRIMARY KEY(`memid`)
database sql

Inserting Data into our Database

Next, we insert sample data into our database to use in our search.

  1. Click vuetot database that we have created earlier.
  2. Click SQL and paste the following codes.
    1. INSERT INTO `members` (`memid`, `firstname`, `lastname`) VALUES
    2. (1, 'neovic', 'devierte'),
    3. (2, 'gemalyn', 'cepe'),
    4. (3, 'gemalyn', 'devierte'),
    5. (4, 'julyn', 'divinagracia');
    6.  
  3. Click Go button below.

Creating the Interface

This contains our sample table and our search input.

index.php
  1. <!DOCTYPE html>
  2. <title>Live Search using Vue.js with PHP</title>
  3. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
  4. <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.10/vue.min.js"></script>
  5. <script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.17.1/axios.min.js"></script>
  6. </head>
  7. <div id="myapp">
  8. <div class="container">
  9. <h1 class="page-header text-center">Live Search using Vue.js with PHP</h1>
  10. <div class="col-md-8 col-md-offset-2">
  11. <div class="row">
  12. <div class="col-md-4 col-md-offset-8">
  13. <input type="text" class="form-control" placeholder="Search Firstname or Lastname" v-on:keyup="searchMonitor" v-model="search.keyword">
  14. </div>
  15. </div>
  16. <div style="height:5px;"></div>
  17. <table class="table table-bordered table-striped">
  18. <th>Firstname</th>
  19. <th>Lastname</th>
  20. </thead>
  21.  
  22. <tr v-if="noMember">
  23. <td colspan="2" align="center">No member match your search</td>
  24. </tr>
  25.  
  26. <tr v-for="member in members">
  27. <td>{{ member.firstname }}</td>
  28. <td>{{ member.lastname }}</td>
  29. </tr>
  30. </tbody>
  31. </table>
  32. </div>
  33. </div>
  34. </div>
  35. <script src="app.js"></script>
  36. </body>
  37. </html>

Creating a Script

This contains our vue.js code.

app.js
  1. var app = new Vue({
  2. el: '#myapp',
  3. data:{
  4. members: [],
  5. search: {keyword: ''},
  6. noMember: false
  7. },
  8.  
  9. mounted: function(){
  10. this.fetchMembers();
  11. },
  12.  
  13. methods:{
  14. searchMonitor: function() {
  15. var keyword = app.toFormData(app.search);
  16. axios.post('action.php?action=search', keyword)
  17. .then(function(response){
  18. app.members = response.data.members;
  19.  
  20. if(response.data.members == ''){
  21. app.noMember = true;
  22. }
  23. else{
  24. app.noMember = false;
  25. }
  26. });
  27. },
  28.  
  29. fetchMembers: function(){
  30. axios.post('action.php')
  31. .then(function(response){
  32. app.members = response.data.members;
  33. });
  34. },
  35.  
  36. toFormData: function(obj){
  37. var form_data = new FormData();
  38. for(var key in obj){
  39. form_data.append(key, obj[key]);
  40. }
  41. return form_data;
  42. },
  43.  
  44. }
  45. });

Creating the API

Lastly, this contains our PHP code in fetching member data from our database.

action.php
  1. <?php
  2. $conn = new mysqli("localhost", "root", "", "vuetot");
  3.  
  4. if ($conn->connect_error) {
  5. die("Connection failed: " . $conn->connect_error);
  6. }
  7.  
  8. $out = array('error' => false);
  9.  
  10. $action="show";
  11.  
  12. if(isset($_GET['action'])){
  13. $action=$_GET['action'];
  14. }
  15.  
  16. if($action=='show'){
  17. $sql = "select * from members";
  18. $query = $conn->query($sql);
  19. $members = array();
  20.  
  21. while($row = $query->fetch_array()){
  22. array_push($members, $row);
  23. }
  24.  
  25. $out['members'] = $members;
  26. }
  27.  
  28. if($action=='search'){
  29. $keyword=$_POST['keyword'];
  30. $sql="select * from members where firstname like '%$keyword%' or lastname like '%$keyword%'";
  31. $query = $conn->query($sql);
  32. $members = array();
  33.  
  34. while($row = $query->fetch_array()){
  35. array_push($members, $row);
  36. }
  37.  
  38. $out['members'] = $members;
  39. }
  40.  
  41. $conn->close();
  42.  
  43. header("Content-type: application/json");
  44. echo json_encode($out);
  45. die();
  46.  
  47. ?>

DEMO

That ends this tutorial. I help you with what you are looking for and you'll find this tutorial useful for your future web application projects.

Happy Coding :)

Add new comment