Anoop
Anoop

Reputation: 23

Calling PHP function using AJAX



I have 3 buttons in my html page, say ADD, UPDATE & DELETE. And I have written a PHP class DBComm in a PHP file dbutils.php which contains three functions add(), update() & delete() for adding, updating & deleting data in the database.

What I want to do is, on submission of the page, I want to create an instance of the PHP class and would be able to call the corresponding PHP function in that class. I wrote the AJAX code,

xmlhttp.open("POST", "dbutils.php", true);

But, if I write like this, I may need to use three php files for handling each event, like Add.php, Update.php & Delete.php. Is there any option in AJAX for creating an instance of the class and to invoke a particular function in that class so that I can keep all the related things in the same place, ie in the class itself?

Upvotes: 2

Views: 2390

Answers (2)

headersalreadysent
headersalreadysent

Reputation: 46

you can send parameters with xmlhttp

xmlHttp.open("POST", "dbutils.php", true); 
var params = "function=" + function + "&paramater=" + parameter; 
xmlHttp.send(params); 

on the php side in dbutils file you can use your function. for example

function add(){}
function delete(){}
function edit(){}
if(in_array($_POST['function'],array('add','edit','delete'))
{
     //call the function
     $_POST['function']();
}

Upvotes: 2

Justin John
Justin John

Reputation: 9635

Pass a variable with ajax for add, update and delete and check with corresponding variable to call the function in php.

Upvotes: 1

Related Questions