Vue.js Insert Data into MySQL Database using PHP

Getting Started

This is the continuation of my tutorial, Vue.js Fetch Data from MySQL Database using PHP, wherein this time we are going to insert data into our database. For database structure, please visit the previous tutorial.

Creating our Add Modal

First we're are going to create our add modal as well as our custom css. 1. Create a new file and name it as modal.php and paste the ff codes:
  1. <!-- Add Modal -->
  2. <div class="myModal" v-if="showAddModal">
  3.         <div class="modalContainer">
  4.                 <div class="modalHeader">
  5.                         <span class="headerTitle">Add New Member</span>
  6.                         <button class="closeBtn pull-right" @click="showAddModal = false">&times;</button>
  7.                 </div>
  8.                 <div class="modalBody">
  9.                         <div class="form-group">
  10.                                 <label>Firstname:</label>
  11.                                 <input type="text" class="form-control" v-model="newMember.firstname">
  12.                         </div>
  13.                         <div class="form-group">
  14.                                 <label>Lastname:</label>
  15.                                 <input type="text" class="form-control" v-model="newMember.lastname">
  16.                         </div>
  17.                 </div>
  18.                 <hr>
  19.                 <div class="modalFooter">
  20.                         <div class="footerBtn pull-right">
  21.                                 <button class="btn btn-default" @click="showAddModal = false"><span class="glyphicon glyphicon-remove"></span> Cancel</button> <button class="btn btn-primary" @click="showAddModal = false; saveMember();"><span class="glyphicon glyphicon-floppy-disk"></span> Save</button>
  22.                         </div>
  23.                 </div>
  24.         </div>
  25. </div>
2. Create a new file and name it as style.css and paste the ff. styles:
  1. .myModal{
  2.         position:fixed;
  3.         top:0;
  4.         left:0;
  5.         right:0;
  6.         bottom:0;
  7.         background: rgba(0, 0, 0, 0.4);
  8. }
  9.  
  10. .modalContainer{
  11.         width: 555px;
  12.         background: #FFFFFF;
  13.         margin:auto;
  14.         margin-top:50px;
  15. }
  16.  
  17. .modalHeader{
  18.         padding:10px;
  19.         background: #008CBA;
  20.         color: #FFFFFF;
  21.         height:50px;
  22.         font-size:20px;
  23.         padding-left:15px;
  24. }
  25.  
  26.  
  27. .modalBody{
  28.         padding:40px;
  29. }
  30.  
  31. .modalFooter{
  32.         height:36px;
  33. }
  34.  
  35. .footerBtn{
  36.         margin-right:10px;
  37.         margin-top:-9px;
  38. }
  39.  
  40. .closeBtn{
  41.         background: #008CBA;
  42.         color: #FFFFFF;
  43.         border:none;
  44. }

Updating index.php

Next, we update our index to accomodate our modal and add error or success div.
  1. <!DOCTYPE html>
  2.         <title>Vue.js CRUD Operation using PHP/MySQLi</title>
  3.         <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
  4.         <link rel="stylesheet" type="text/css" href="style.css">
  5. </head>
  6. <div class="container">
  7.         <h1 class="page-header text-center">Vue.js CRUD Operation with PHP/MySQLi</h1>
  8.         <div id="members">
  9.                 <div class="col-md-8 col-md-offset-2">
  10.                         <div class="row">
  11.                                 <div class="col-md-12">
  12.                                         <h2>Member List
  13.                                         <button class="btn btn-primary pull-right" @click="showAddModal = true"><span class="glyphicon glyphicon-plus"></span> Member</button>
  14.                                         </h2>
  15.                                 </div>
  16.                         </div>
  17.  
  18.                         <div class="alert alert-danger text-center" v-if="errorMessage">
  19.                                 <button type="button" class="close" @click="clearMessage();"><span aria-hidden="true">&times;</span></button>
  20.                                 <span class="glyphicon glyphicon-alert"></span> {{ errorMessage }}
  21.                         </div>
  22.                        
  23.                         <div class="alert alert-success text-center" v-if="successMessage">
  24.                                 <button type="button" class="close" @click="clearMessage();"><span aria-hidden="true">&times;</span></button>
  25.                                 <span class="glyphicon glyphicon-ok"></span> {{ successMessage }}
  26.                         </div>
  27.  
  28.                         <table class="table table-bordered table-striped">
  29.                                 <thead>
  30.                                         <th>Firstname</th>
  31.                                         <th>Lastname</th>
  32.                                         <th>Action</th>
  33.                                 </thead>
  34.                                 <tbody>
  35.                                         <tr v-for="member in members">
  36.                                                 <td>{{ member.firstname }}</td>
  37.                                                 <td>{{ member.lastname }}</td>
  38.                                                 <td>
  39.                                                         <button class="btn btn-success"><span class="glyphicon glyphicon-edit"></span> Edit</button> <button class="btn btn-danger"><span class="glyphicon glyphicon-trash"></span> Delete</button>
  40.  
  41.                                                 </td>
  42.                                         </tr>
  43.                                 </tbody>
  44.                         </table>
  45.                 </div>
  46.  
  47.                 <?php include('modal.php'); ?>
  48.         </div>
  49. </div>
  50. <script src="vue.js"></script>
  51. <script src="axios.js"></script>
  52. <script src="app.js"></script>
  53. </body>
  54. </html>

Setting up our Add API

In our api.php, add the following line after the if statement for our read.
  1. if($crud == 'create'){
  2.  
  3.         $firstname = $_POST['firstname'];
  4.         $lastname = $_POST['lastname'];
  5.  
  6.         $sql = "insert into members (firstname, lastname) values ('$firstname', '$lastname')";
  7.         $query = $conn->query($sql);
  8.  
  9.         if($query){
  10.                 $out['message'] = "Member Added Successfully";
  11.         }
  12.         else{
  13.                 $out['error'] = true;
  14.                 $out['message'] = "Could not add Member";
  15.         }
  16.        
  17. }

Updating our Vue Code

Lastly, we update our app.js which contains our vue code with the ff:
  1. var app = new Vue({
  2.         el: '#members',
  3.         data:{
  4.                 showAddModal: false,
  5.                 errorMessage: "",
  6.                 successMessage: "",
  7.                 members: [],
  8.                 newMember: {firstname: '', lastname: ''}
  9.         },
  10.  
  11.         mounted: function(){
  12.                 this.getAllMembers();
  13.         },
  14.  
  15.         methods:{
  16.                 getAllMembers: function(){
  17.                         axios.get('api.php')
  18.                                 .then(function(response){
  19.                                         //console.log(response);
  20.                                         if(response.data.error){
  21.                                                 app.errorMessage = response.data.message;
  22.                                         }
  23.                                         else{
  24.                                                 app.members = response.data.members;
  25.                                         }
  26.                                 });
  27.                 },
  28.  
  29.                 saveMember: function(){
  30.                         //console.log(app.newMember);
  31.                         var memForm = app.toFormData(app.newMember);
  32.                         axios.post('api.php?crud=create', memForm)
  33.                                 .then(function(response){
  34.                                         //console.log(response);
  35.                                         app.newMember = {firstname: '', lastname:''};
  36.                                         if(response.data.error){
  37.                                                 app.errorMessage = response.data.message;
  38.                                         }
  39.                                         else{
  40.                                                 app.successMessage = response.data.message
  41.                                                 app.getAllMembers();
  42.                                         }
  43.                                 });
  44.                 },
  45.  
  46.                 toFormData: function(obj){
  47.                         var form_data = new FormData();
  48.                         for(var key in obj){
  49.                                 form_data.append(key, obj[key]);
  50.                         }
  51.                         return form_data;
  52.                 },
  53.  
  54.                 clearMessage: function(){
  55.                         app.errorMessage = '';
  56.                         app.successMessage = '';
  57.                 }
  58.  
  59.         }
  60. });
You should now be able to add new row using vue.js That ends this tutorial. Happy Coding :)

Add new comment