2



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?

2 Answers 2

2

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']();
}
Sign up to request clarification or add additional context in comments.

Comments

1

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

1 Comment

means, like an event handler php file ? action keys are to be passed to the handler ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.