ajax request for inserting php data to db

Solutions on MaxInterview for ajax request for inserting php data to db by the best coders in the world

showing results for - "ajax request for inserting php data to db"
Luisa
30 Nov 2020
1<!DOCTYPE html>
2<html>
3<head>
4	<title>Insert data in MySQL database using Ajax</title>
5	<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
6	<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
7</head>
8<body>
9<div style="margin: auto;width: 60%;">
10	<div class="alert alert-success alert-dismissible" id="success" style="display:none;">
11	  <a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
12	</div>
13	<form id="fupForm" name="form1" method="post">
14		<div class="form-group">
15			<label for="email">Name:</label>
16			<input type="text" class="form-control" id="name" placeholder="Name" name="name">
17		</div>
18		<div class="form-group">
19			<label for="pwd">Email:</label>
20			<input type="email" class="form-control" id="email" placeholder="Email" name="email">
21		</div>
22		<div class="form-group">
23			<label for="pwd">Phone:</label>
24			<input type="text" class="form-control" id="phone" placeholder="Phone" name="phone">
25		</div>
26		<div class="form-group" >
27			<label for="pwd">City:</label>
28			<select name="city" id="city" class="form-control">
29				<option value="">Select</option>
30				<option value="Delhi">Delhi</option>
31				<option value="Mumbai">Mumbai</option>
32				<option value="Pune">Pune</option>
33			</select>
34		</div>
35		<input type="button" name="save" class="btn btn-primary" value="Save to database" id="butsave">
36	</form>
37</div>
38
39<script>
40$(document).ready(function() {
41	$('#butsave').on('click', function() {
42		$("#butsave").attr("disabled", "disabled");
43		var name = $('#name').val();
44		var email = $('#email').val();
45		var phone = $('#phone').val();
46		var city = $('#city').val();
47		if(name!="" && email!="" && phone!="" && city!=""){
48			$.ajax({
49				url: "save.php",
50				type: "POST",
51				data: {
52					name: name,
53					email: email,
54					phone: phone,
55					city: city				
56				},
57				cache: false,
58				success: function(dataResult){
59					var dataResult = JSON.parse(dataResult);
60					if(dataResult.statusCode==200){
61						$("#butsave").removeAttr("disabled");
62						$('#fupForm').find('input:text').val('');
63						$("#success").show();
64						$('#success').html('Data added successfully !'); 						
65					}
66					else if(dataResult.statusCode==201){
67					   alert("Error occured !");
68					}
69					
70				}
71			});
72		}
73		else{
74			alert('Please fill all the field !');
75		}
76	});
77});
78</script>
79</body>
80</html>
81