AJAX HTML Javascript jQuery PHP Example MORE

PHP Web service example for Cordova insert or save data.


Here we using 3 file for insert or save data using php web service.

  1. index.html
  2. database.php
  3. save.php

index.php

<!doctype html>
<html lang="en">
  <head>		
		<form action="#" id="student_form" method="post">
					<div class="mdl-textfield mdl-js-textfield mdl-textfield--floating-label" style="width:100% !important">
						<input class="mdl-textfield__input" type="text" name="name">
						<label class="mdl-textfield__label" for="sample3">Name</label>
				  	</div>
				  	<div class="mdl-textfield mdl-js-textfield mdl-textfield--floating-label" style="width:100% !important">
						<input class="mdl-textfield__input" type="text" name="email">
						<label class="mdl-textfield__label" for="sample3">Email</label>
				  	</div>
				  	<div class="mdl-textfield mdl-js-textfield mdl-textfield--floating-label" style="width:100% !important">
						<input class="mdl-textfield__input" type="text" name="phone">
						<label class="mdl-textfield__label" for="sample3">Phone</label>
				  	</div>
				  	<button type="button" class="mdl-button mdl-js-button mdl-button--raised mdl-button--accent" name="save">Save</button>
		</form>
		<script>
		$(document).on('click','#save',function(e) {
				var data = $("#student_form").serialize();
				$.ajax({
					 data: data,
					 type: "post",
					 url: "http://www.exampl.com/save.php",
					 success: function(data){
						if(data==200){
							alert("Data added successfully !");
							location.reload();
						}
					 }
				});
		});
		</script>
<script src="https://code.getmdl.io/1.3.0/material.min.js"></script>
</body>
</html>
  

database.php

<?php
	$servername = "localhost";
	$username = "root";
	$password = "";
	$db="school";
	$conn = mysqli_connect($servername, $username, $password,$db);
?>

save.php

<?php
include 'database.php';
$name=$_POST['name'];
$email=$_POST['email'];
$phone=$_POST['phone'];
if(isset($_POST['save'])){
	$sql = "INSERT INTO `crud`( `FirstName`, `LastName`) 
	VALUES ('$FirstName','$LastName')";
	if (mysqli_query($conn, $sql)) {
		echo 200;
	} 
	else {
	    echo 201;
	}
	mysqli_close($conn);
}
?>