Hi,
I've been trying to get this to work for several days now and it just won't work. I'm not sure what the process is for getting help, but I would like to submit a support ticket so I can get this resolved quickly.
The problem:
Once I add a custom editor to the grid it creates duplicate records on create, records are not updated when edited even though the grid display shows the edits and the destroy/delete does not work, no records are deleted even though the grid removes the row from the grid. Also, I could not get the combobox to work using a remote data source (json/php) I had to hard code the list in order for it to work, even though my json is working in the browser.
I'm including the code here, but would like to submit the ticket so that I can not only get this fixed but I also want to understand why this doesn't work when it seems like it should. You'll notice that the php is almost directly taken from the 2 tutorials on the blog. However, and I'll use this to rant a bit, there are no examples in php for deleting a record. Please try and include complete crud examples!
Thanks for looking and please contact me about scheduling a ticket.
Tony
Data-source declaration:
var expense = new kendo.data.DataSource({
transport: {
read: {
url: "../data/expenses.php?StatementID=<?php echo $_SESSION['StatementID']; ?>",
dataType: "json",
type: "GET"
},
create: {
url: "../data/expenses-add.php?StatementID=<?php echo $_SESSION['StatementID']; ?>",
dataType: "json",
type: "PUT"
},
update: {
url: "../data/expenses-update.php",
dataType: "json",
type: "POST"
},
destroy: {
url: "../data/expenses-delete.php",
dataType: "json",
type: "DELETE"
}
},
pageSize: 50,
autoSync: false,
schema: {
data: "data",
model: {
id: "ID",
fields: {
ID: {editable: false } ,
StatementID: { editable: false},
ExpenseType: "Type",
ExpenseAmount: {editable: true, type: "number" },
Notes: {editable: true, type: "string" }
}
}
}
});
Tried this to feed the combobox but couldn't get it to work:
/* var typeDataSource = new kendo.data.DataSource({
transport: {
read: "../data/expense-types.php"
}
}); */
datasource for combobox(works fine, but I need this to work with the above)
var types = [ {
"ID": 1,
"Expense": "Airfare"
}, {
"ID": 2,
"Expense": "Ground Trasportation"
}, {
"ID": 2,
"Expense": "FedEx"
}, {
"ID": 2,
"Expense": "Side-man Pay"
}, {
"ID": 2,
"Expense": "Hotel"
}, {
"ID": 2,
"Expense": "Conference Fee-Charge"
}, {
"ID": 2,
"Expense": "Advance"
}];
Grid declaration:
$("#expenses").kendoGrid({
dataSource: expense,
pageable: true,
sortable: true,
autoBind: true,
height: 400,
toolbar: ["create"],
columns: [
{ field: "ExpenseType", title:"Type", width: "150px", editor: function (container, options) {
$('<input data-text-field="Expense" data-value-field="Expense" data-bind="value:' + options.field + '"/>')
.appendTo(container)
.kendoComboBox({
autoBind: false,
dataSource: types
});
}
},
{ field: "ExpenseAmount", title:"Amount", width: "75px", format: "{0:c}" },
{ field: "Notes", title:"Note", width: "200px" },
{ command: ["edit", "destroy"], title: " ", width: "150px" }
],
editable: "popup"
});
PHP to handle the Update (POST):
<?php
$link = mysql_pconnect("localhost", "user", "pass") or die("Unable To Connect To Database Server");
mysql_select_db("db") or die("Unable To Connect To DB");
// add the header line to specify that the content type is JSON
header("Content-type: application/json");
// determine the request type
$verb = $_SERVER["REQUEST_METHOD"];
// handle a POST
if ($verb == "POST") {
// get the parameters from the post. escape them to protect against sql injection.
$Id = mysql_real_escape_string($_POST["ID"]);
$ExpenseType = mysql_real_escape_string($_POST["ExpenseType"]);
$ExpenseAmount = mysql_real_escape_string($_POST["ExpenseAmount"]);
$Notes = mysql_real_escape_string($_POST["Notes"]);
$rs = mysql_query("UPDATE Expenses SET `ExpenseType` = '".$ExpenseType."', `ExpenseAmount` = '".$ExpenseAmount."', `Notes` = '".$Notes."' WHERE `ID` = '".$ID."'");
if ($rs) {
echo "" .$Id. "";
}
else {
header("HTTP/1.1 500 Internal Server Error");
echo "Update failed for ID: " .$Id;
}
}
?>
PHP to handle the Create (PUT):
<?php
$link = mysql_pconnect("localhost", "user", "pass") or die("Unable To Connect To Database Server");
mysql_select_db("db") or die("Unable To Connect To DB");
// add the header line to specify that the content type is JSON
header("Content-type: application/json");
// determine the request type
$verb = $_SERVER["REQUEST_METHOD"];
// handle a Put
if ($verb == "PUT") {
$request_vars = Array();
parse_str(file_get_contents('php://input'), $request_vars );
//get the parameters from the post. escape them to protect against sql injection.
$ExpenseType = mysql_real_escape_string($request_vars["ExpenseType"]);
$ExpenseAmount = mysql_real_escape_string($request_vars["ExpenseAmount"]);
$Notes = mysql_real_escape_string($request_vars["Notes"]);
$StatementID = mysql_real_escape_string($_GET["StatementID"]);
$sql = "INSERT INTO Expenses (`StatementID`, `ExpenseType`, `ExpenseAmount`, `Notes`) VALUES ('".$StatementID."', '".$ExpenseType."', '".$ExpenseAmount."', '".$Notes."')";
$rs = mysql_query($sql);
if ($rs) {
$ID = mysql_insert_id();
echo $ID;
}
else {
header("HTTP/1.1 500 Internal Server Error");
echo false;
}
}
?>
PHP to handle Destroy(DELETE):
<?php
$link = mysql_pconnect("localhost", "user", "pass") or die("Unable To Connect To Database Server");
mysql_select_db("db") or die("Unable To Connect To DB");
// add the header line to specify that the content type is JSON
header("Content-type: application/json");
// determine the request type
$verb = $_SERVER["REQUEST_METHOD"];
if ($verb == "DELETE") {
// get the parameters from the post. escape them to protect against sql injection.
$ID = mysql_real_escape_string($_GET["ID"]);
$sql= "DELETE FROM Expenses WHERE ID = '".$ID."'";
$rs = mysql_query($sql);
if ($rs) {
echo "".$ID."";
}
else {
header("HTTP/1.1 500 Internal Server Error");
echo false;
}
}
?>
I've been trying to get this to work for several days now and it just won't work. I'm not sure what the process is for getting help, but I would like to submit a support ticket so I can get this resolved quickly.
The problem:
Once I add a custom editor to the grid it creates duplicate records on create, records are not updated when edited even though the grid display shows the edits and the destroy/delete does not work, no records are deleted even though the grid removes the row from the grid. Also, I could not get the combobox to work using a remote data source (json/php) I had to hard code the list in order for it to work, even though my json is working in the browser.
I'm including the code here, but would like to submit the ticket so that I can not only get this fixed but I also want to understand why this doesn't work when it seems like it should. You'll notice that the php is almost directly taken from the 2 tutorials on the blog. However, and I'll use this to rant a bit, there are no examples in php for deleting a record. Please try and include complete crud examples!
Thanks for looking and please contact me about scheduling a ticket.
Tony
Data-source declaration:
var expense = new kendo.data.DataSource({
transport: {
read: {
url: "../data/expenses.php?StatementID=<?php echo $_SESSION['StatementID']; ?>",
dataType: "json",
type: "GET"
},
create: {
url: "../data/expenses-add.php?StatementID=<?php echo $_SESSION['StatementID']; ?>",
dataType: "json",
type: "PUT"
},
update: {
url: "../data/expenses-update.php",
dataType: "json",
type: "POST"
},
destroy: {
url: "../data/expenses-delete.php",
dataType: "json",
type: "DELETE"
}
},
pageSize: 50,
autoSync: false,
schema: {
data: "data",
model: {
id: "ID",
fields: {
ID: {editable: false } ,
StatementID: { editable: false},
ExpenseType: "Type",
ExpenseAmount: {editable: true, type: "number" },
Notes: {editable: true, type: "string" }
}
}
}
});
Tried this to feed the combobox but couldn't get it to work:
/* var typeDataSource = new kendo.data.DataSource({
transport: {
read: "../data/expense-types.php"
}
}); */
datasource for combobox(works fine, but I need this to work with the above)
var types = [ {
"ID": 1,
"Expense": "Airfare"
}, {
"ID": 2,
"Expense": "Ground Trasportation"
}, {
"ID": 2,
"Expense": "FedEx"
}, {
"ID": 2,
"Expense": "Side-man Pay"
}, {
"ID": 2,
"Expense": "Hotel"
}, {
"ID": 2,
"Expense": "Conference Fee-Charge"
}, {
"ID": 2,
"Expense": "Advance"
}];
Grid declaration:
$("#expenses").kendoGrid({
dataSource: expense,
pageable: true,
sortable: true,
autoBind: true,
height: 400,
toolbar: ["create"],
columns: [
{ field: "ExpenseType", title:"Type", width: "150px", editor: function (container, options) {
$('<input data-text-field="Expense" data-value-field="Expense" data-bind="value:' + options.field + '"/>')
.appendTo(container)
.kendoComboBox({
autoBind: false,
dataSource: types
});
}
},
{ field: "ExpenseAmount", title:"Amount", width: "75px", format: "{0:c}" },
{ field: "Notes", title:"Note", width: "200px" },
{ command: ["edit", "destroy"], title: " ", width: "150px" }
],
editable: "popup"
});
PHP to handle the Update (POST):
<?php
$link = mysql_pconnect("localhost", "user", "pass") or die("Unable To Connect To Database Server");
mysql_select_db("db") or die("Unable To Connect To DB");
// add the header line to specify that the content type is JSON
header("Content-type: application/json");
// determine the request type
$verb = $_SERVER["REQUEST_METHOD"];
// handle a POST
if ($verb == "POST") {
// get the parameters from the post. escape them to protect against sql injection.
$Id = mysql_real_escape_string($_POST["ID"]);
$ExpenseType = mysql_real_escape_string($_POST["ExpenseType"]);
$ExpenseAmount = mysql_real_escape_string($_POST["ExpenseAmount"]);
$Notes = mysql_real_escape_string($_POST["Notes"]);
$rs = mysql_query("UPDATE Expenses SET `ExpenseType` = '".$ExpenseType."', `ExpenseAmount` = '".$ExpenseAmount."', `Notes` = '".$Notes."' WHERE `ID` = '".$ID."'");
if ($rs) {
echo "" .$Id. "";
}
else {
header("HTTP/1.1 500 Internal Server Error");
echo "Update failed for ID: " .$Id;
}
}
?>
PHP to handle the Create (PUT):
<?php
$link = mysql_pconnect("localhost", "user", "pass") or die("Unable To Connect To Database Server");
mysql_select_db("db") or die("Unable To Connect To DB");
// add the header line to specify that the content type is JSON
header("Content-type: application/json");
// determine the request type
$verb = $_SERVER["REQUEST_METHOD"];
// handle a Put
if ($verb == "PUT") {
$request_vars = Array();
parse_str(file_get_contents('php://input'), $request_vars );
//get the parameters from the post. escape them to protect against sql injection.
$ExpenseType = mysql_real_escape_string($request_vars["ExpenseType"]);
$ExpenseAmount = mysql_real_escape_string($request_vars["ExpenseAmount"]);
$Notes = mysql_real_escape_string($request_vars["Notes"]);
$StatementID = mysql_real_escape_string($_GET["StatementID"]);
$sql = "INSERT INTO Expenses (`StatementID`, `ExpenseType`, `ExpenseAmount`, `Notes`) VALUES ('".$StatementID."', '".$ExpenseType."', '".$ExpenseAmount."', '".$Notes."')";
$rs = mysql_query($sql);
if ($rs) {
$ID = mysql_insert_id();
echo $ID;
}
else {
header("HTTP/1.1 500 Internal Server Error");
echo false;
}
}
?>
PHP to handle Destroy(DELETE):
<?php
$link = mysql_pconnect("localhost", "user", "pass") or die("Unable To Connect To Database Server");
mysql_select_db("db") or die("Unable To Connect To DB");
// add the header line to specify that the content type is JSON
header("Content-type: application/json");
// determine the request type
$verb = $_SERVER["REQUEST_METHOD"];
if ($verb == "DELETE") {
// get the parameters from the post. escape them to protect against sql injection.
$ID = mysql_real_escape_string($_GET["ID"]);
$sql= "DELETE FROM Expenses WHERE ID = '".$ID."'";
$rs = mysql_query($sql);
if ($rs) {
echo "".$ID."";
}
else {
header("HTTP/1.1 500 Internal Server Error");
echo false;
}
}
?>