this is my botton:
<form>
<input type="hidden" id="id" value="'.$row['order_id'].'">
<input type="button" value="Delete" onclick="ajaxFunction()">
</form>
this is the ajax function:
<script language="javascript" type="text/javascript">
<!--
//Browser Support Code
function ajaxFunction(){
var ajaxRequest; // The variable that makes Ajax possible!
try{
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
} catch (e){
// Internet Explorer Browsers
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
// Create a function that will receive data sent from the server
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
document.myForm.time.value = ajaxRequest.responseText;
}
}
var id = document.getElementById(\'id\').value;
var queryString = "?id=" + id;
ajaxRequest.open("GET", "classes/ajaxed/reservation_functions.php" + queryString, true);
ajaxRequest.send(null);
}
//-->
</script>
and here is the php file that handles this function called reservation_functions.php:
<?php
$id = $_GET['id'];
require_once '../class.db.php';
require_once '../../includes/constants.php';
$db = new MySQL(DB_SERVER, DB_USER, DB_PASSWORD, DB_NAME, false);
$db->query("DELETE FROM orders WHERE order_id = $id;") or die(mysql_error());
echo 'DONE!';
?>
I am really good with PHP but I am really far from AJAX so my question is how to handle different parameters with ajax, to be more clear: I want to add a delete function, Print function, Edit function but I want to make one php file to handle all the functions and those function to come with AJAX.
It is a little bit hard for me to come up with idea because I am really far from AJAX,
Thank you for your time and help.
How about:
switch (strtolower($_GET['action'])) {
case 'delete' :
// delete …
break;
case 'add' :
// add …
break;
case 'foobar' :
// foobar …
break;
default :
// 404
}
Then call the URL with the additional action
parameter: ?action=delete&...
.
you can do it:
Class:
class AjaxActions {
private static $instance;
private function __construct () {}
public static class getInstance() {
if (!self::$instance) self:$instance = new self();
return self::$instance;
}
private function makeReturn ($action, $data) {
return json_encode(array(
'action' => (string) $action,
'time' => time(),
'result' => $data
));
}
public function delete ($request) {
$db = new MySQL(DB_SERVER, DB_USER, DB_PASSWORD, DB_NAME, false);
$id = (int) $request['id'];
$db->query("DELETE FROM orders WHERE order_id = $id;");
if (!$db->errn) {
$error = 0;
$emsg = "";
} else {
$error = $db->errn;
$emsg = $db->error;
}
return $this->makeReturn('delete', array(
'error' => $error,
'errorMessage' => $emsg
));
}
// Other actions here..
}
And...
$action = isset($_REQUEST['action']) ? $_REQUEST['action'] : null;
if ($action) {
$oAjaxCommand = AjaxActions::getInstace();
if (method_exists($oAjaxCommand,$action) {
$response = call_user_func (array($oAjaxCommand,$action),$_REQUEST);
echo $response;
exit(0);
} else {
thrown new Exception("Action not exists");
}
}
From de javascript side, you resive a json object with the response. I recomended use a JS framework like mootools or jquery. This frameworks do the ajax work much easy, and cross-browser.