<?php
class InsertData
{
private $fname, $lname, $phone, $email_address;
#This is the class constructor
public function __construct($fname, $lname, $phone, $email_address)
{
$this->fname = $fname;
$this->lname = $lname;
$this->phone = $phone;
$this->email_address = $email_address;
$this->insertQuery(); //Instanciating the object will automatically call the insert statement
}
public function insertQuery()
{
$sql = "INSERT INTO address_bk(fname, lname, phone, email_address)
VALUES('$this->fname', '$this->lname', '$this->phone', '$this->email_address')";
$result = mysql_query($sql);
//If record has been successfully added
if($result)
{
echo "Contact has been successfully added";
}
else
{
echo "Error! Inserting new contact";
}
}
}
include_once 'connection.php';
//Instantiate the class(InsertData class call)
new InsertData($_GET['fname'], $_GET['lname'], $_GET['phone'], $_GET['email']);
//Close connection after transaction
mysql_close($server_connect);
?>
class InsertData
{
private $fname, $lname, $phone, $email_address;
#This is the class constructor
public function __construct($fname, $lname, $phone, $email_address)
{
$this->fname = $fname;
$this->lname = $lname;
$this->phone = $phone;
$this->email_address = $email_address;
$this->insertQuery(); //Instanciating the object will automatically call the insert statement
}
public function insertQuery()
{
$sql = "INSERT INTO address_bk(fname, lname, phone, email_address)
VALUES('$this->fname', '$this->lname', '$this->phone', '$this->email_address')";
$result = mysql_query($sql);
//If record has been successfully added
if($result)
{
echo "Contact has been successfully added";
}
else
{
echo "Error! Inserting new contact";
}
}
}
include_once 'connection.php';
//Instantiate the class(InsertData class call)
new InsertData($_GET['fname'], $_GET['lname'], $_GET['phone'], $_GET['email']);
//Close connection after transaction
mysql_close($server_connect);
?>
Create a javaScript file and name it ajax_add.js
var xmlHttp = createXMLHttpRequest();
function createXMLHttpRequest()
{
if(window.ActiveXObject)
{
return new ActiveXObject("Microsoft.XMLHTTP");
}
else if(window.XMLHttpRequest)
{
return new XMLHttpRequest();
}
}
function handleStateChange()
{
if(xmlHttp.readyState == 4)
{
if(xmlHttp.status == 200)
{
document.getElementById('content').innerHTML = xmlHttp.responseText;
}
}
}
function submitData()
{
var fname = document.getElementById('fname_post').value;
var lname = document.getElementById('lname_post').value;
var phone = document.getElementById('phone_post').value;
var email = document.getElementById('email_post').value;
var url = "http://localhost/samples/add_record.php?fname=" + escape(fname) + "&lname=" + escape(lname) + "&phone=" + escape(phone) + "&email=" + escape(email);
xmlHttp.open("GET", url, true);
xmlHttp.onreadystatechange = handleStateChange;
xmlHttp.send(null);
xmlHttp.close();
}
Create a html file and name it add_record.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Insert using Ajax Data Demo</title>
<script type="text/javascript" src="ajax_add.js"></script>
</head>
<body>
<h2>Insert using Ajax Data Demo</h2>
<table>
<tr>
<td>Firstname</td>
<td><input type="text" id="fname_post" value="" /></td>
</tr>
<tr>
<td>Lastname</td>
<td><input type="text" id="lname_post" value="" /></td>
</tr>
<tr>
<td>Phone number</td>
<td><input type="text" id="phone_post" value="" /></td>
</tr>
<tr>
<td>Email Address</td>
<td><input type="text" id="email_post" value="" /></td>
</tr>
<tr>
<td></td>
<td>
<input type="submit" value="Add Contact" onClick="submitData()" />
</td>
</tr>
</table>
<div id="content">
</div>
</body>
</html>
As you can see from the above page display, the page is rendered through a html page. The ajax script is responsible for routing requests to the php server side script.
The figure below represents the database table structure.