Tagged: crud operations in php using ajax

VueJS CRUD Operations with PHP and MySQLi Example 0

VueJS CRUD Operations with PHP and MySQLi Example

VueJS CRUD Operations with PHP and MySQLi Example

In this Post We Will Explain About is VueJS CRUD Operations with PHP and MySQLi Example With Example and Demo.Welcome on Pakainfo.com – Examples, The best For Learn web development Tutorials,Demo with Example! Hi Dear Friends here u can know to vue Select Insert Update Delete In Php MySQL Example

In this post we will show you Best way to implement Vue.js Update and Delete Data using PHP, hear for VueJS insert, view, edit, delete and update using PHP and Mysql with Download .we will give you demo,Source Code and examples for implement Step By Step Good Luck!.

Vue.js CRUD Operation with PHP/MySQLi

In this Example,First of all Add or Include External Libs Like as a(jQuery, css etc..), and then create a simple index.php or index.html page.After that crate a simple javascript file like as a index.js or main.js, It is also add your web-application First Header Part to some priority set.After that Include your relevant CSS Class.

modal.php

<!--Pakainfo.com Edit Modal -->
<div class="myModal" v-if="usershowEditmdl">
	<div class="modalContainer">
		<div class="headerEdit">
			<span class="live headerTitle">Edit User</span>
			<button class="live editCloseBtn pull-right" @click="usershowEditmdl = false">×</button>
		</div>
		<div class="modalBody">
			<div class="live form-group">
				<label>User Firstname:</label>
				<input type="text" class="form-control" v-model="userClick.userfname">
			</div>
			<div class="live form-group">
				<label>User Lastname:</label>
				<input type="text" class="form-control" v-model="userClick.userlname">
			</div>
		</div>
		<hr>
		<div class="modalFooter">
			<div class="footerBtn pull-right">
				<button class="btn btn-default" @click="usershowEditmdl = false"><span class="glyphicon glyphicon-remove"></span> Cancel</button> <button class="btn btn-success" @click="usershowEditmdl = false; updateUser();"><span class="glyphicon glyphicon-check"></span> Save</button>
			</div>
		</div>
	</div>
</div>
 
<!-- Remove Modal -->
<div class="myModal" v-if="showDeleteModal">
	<div class="modalContainer">
		<div class="modelheaderdel">
			<span class="headerTitle">Remove User</span>
			<button class="removeCloseBtn pull-right" @click="showDeleteModal = false">×</button>
		</div>
		<div class="modalBody">
			<h5 class="text-right">You want to Remove User</h5>
			<h2 class="text-right">{{userClick.userfname}} {{userClick.userlname}}</h2>
		</div>
		<hr>
		<div class="modalFooter">
			<div class="footerBtn pull-right">
				<button class="btn btn-default" @click="showDeleteModal = false"><span class="live glyphicon glyphicon-remove"></span> Cancel</button> <button class="live btn btn-danger" @click="showDeleteModal = false; removeUser(); "><span class="live glyphicon glyphicon-trash"></span> Yes</button>
			</div>
		</div>
	</div>
</div>

style.css

.headerEdit{
	height:45px;
	font-size:20px;
	padding-left:18px;
	padding:8px;
	background: #4CAF50;
	color: #c60000;
}
 
.modelheaderdel{
	height:45px;
	font-size:20px;
	padding-left:18px;
	padding:8px;
	background: #3d3d3d;
	color: #c60000;
}
 
.editCloseBtn{
	background: #4CAF50;
	color: #c60000;
	border:none;
}
 
.removeCloseBtn{
	background: #3d3d3d;
	color: #c60000;
	border:none;
}

Updating our index.php

<!DOCTYPE html>
<html>
<head>
	<title>Vue.js Insert, update and delete Operation using PHP/MySQLi</title>
	css/bootstrap.min.css"
	<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div class="container">
	<h1 class="page-header text-right">Vue.js Insert Update Delete Operation with PHP/MySQLi</h1>
	<div id="users">
		<div class="col-md-8 col-md-offset-2">
			<div class="row">
				<div class="col-md-12">
					<h2>User List
					<button class="live btn btn-primary pull-right" @click="usershowaddmdl = true"><span class="live glyphicon glyphicon-plus"></span> User</button>
					</h2>
				</div>
			</div>
 
			<div class="live alert alert-danger text-right" v-if="msgError">
				<button type="button" class="close" @click="clsMsg();"><span aria-hidden="true">×</span></button>
				<span class="live glyphicon glyphicon-alert"></span> {{ msgError }}
			</div>
 
			<div class="live alert alert-success text-right" v-if="msgsuccess">
				<button type="button" class="close" @click="clsMsg();"><span aria-hidden="true">×</span></button>
				<span class="live glyphicon glyphicon-ok"></span> {{ msgsuccess }}
			</div>
 
			<table class="table table-bordered table-striped">
				<thead>
					<th>User Firstname</th>
					<th>User Lastname</th>
					<th>Action</th>
				</thead>
				<tbody>
					<tr v-for="user in users">
						<td>{{ user.userfname }}</td>
						<td>{{ user.userlname }}</td>
						<td>
							<button class="btn btn-success" @click="usershowEditmdl = true; selectUser(user);"><span class="glyphicon glyphicon-edit"></span> Edit</button> <button class="btn btn-danger" @click="showDeleteModal = true; selectUser(user);"><span class="glyphicon glyphicon-trash"></span> Remove</button>
 
						</td>
					</tr>
				</tbody>
			</table>
		</div>
 
		<?php include('modal.php'); ?>
	</div>
</div>
<script src="vue.js"></script>
<script src="axios.js"></script>
<script src="main.js"></script>
</body>
</html>

Adding Update and Remove Code in our API

api.php

if($crudopration == 'update'){
 
	$userid = $_POST['userid'];
	$userfname = $_POST['userfname'];
	$userlname = $_POST['userlname'];
 
	$sql = "update users set userfname='$userfname', userlname='$userlname' where userid='$userid'";
	$query = $conn->query($sql);
 
	if($query){
		$out['msg'] = "User Updated Successfully";
	}
	else{
		$out['error'] = true;
		$out['msg'] = "Could not update User";
	}
 
}
 
if($crudopration == 'delete'){
 
	$userid = $_POST['userid'];
 
	$sql = "delete from users where userid='$userid'";
	$query = $conn->query($sql);
 
	if($query){
		$out['msg'] = "User Deleted Successfully";
	}
	else{
		$out['error'] = true;
		$out['msg'] = "Could not delete User";
	}
 
}

Updating our Vue Code

main.js

var main = new Vue({
	el: '#users',
	data:{
		usershowaddmdl: false,
		usershowEditmdl: false,
		showDeleteModal: false,
		msgError: "",
		msgsuccess: "",
		users: [],
		newUser: {userfname: '', userlname: ''},
		userClick: {}
	},
 
	mounted: function(){
		this.getAllUsers();
	},
 
	methods:{
		getAllUsers: function(){
			axios.get('api.php')
				.then(function(results){
					//console.log(results);
					if(results.data.error){
						main.msgError = results.data.msg;
					}
					else{
						main.users = results.data.users;
					}
				});
		},
 
		saveUser: function(){
			//console.log(main.newUser);
			var usrForm = main.myFormData(main.newUser);
			axios.post('api.php?crudopration=create', usrForm)
				.then(function(results){
					//console.log(results);
					main.newUser = {userfname: '', userlname:''};
					if(results.data.error){
						main.msgError = results.data.msg;
					}
					else{
						main.msgsuccess = results.data.msg
						main.getAllUsers();
					}
				});
		},
 
		updateUser(){
			var usrForm = main.myFormData(main.userClick);
			axios.post('api.php?crudopration=update', usrForm)
				.then(function(results){
					//console.log(results);
					main.userClick = {};
					if(results.data.error){
						main.msgError = results.data.msg;
					}
					else{
						main.msgsuccess = results.data.msg
						main.getAllUsers();
					}
				});
		},
 
		removeUser(){
			var usrForm = main.myFormData(main.userClick);
			axios.post('api.php?crudopration=delete', usrForm)
				.then(function(results){
					//console.log(results);
					main.userClick = {};
					if(results.data.error){
						main.msgError = results.data.msg;
					}
					else{
						main.msgsuccess = results.data.msg
						main.getAllUsers();
					}
				});
		},
 
		selectUser(user){
			main.userClick = user;
		},
 
		myFormData: function(obj){
			var dataForm = new FormData();
			for(var key in obj){
				dataForm.append(key, obj[key]);
			}
			return dataForm;
		},
 
		clsMsg: function(){
			main.msgError = '';
			main.msgsuccess = '';
		}
 
	}
});

VueJS INSERT, UPDATE and DELETE

You are Most welcome in my youtube Channel Please subscribe my channel. and give me FeedBack.
More Details……
Angularjs Example

Example

I hope you have Got What is vue Select Insert Update Delete In Php MySQL And how it works.I would Like to have FeedBack From My Blog(Pakainfo.com) readers.Your Valuable FeedBack,Any Question,or any Comments about This Article(Pakainfo.com) Are Most Always Welcome.