This is a migrated thread and some comments may be shown as answers.

CRUD actions - what should server response be?

14 Answers 956 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Mat
Top achievements
Rank 1
Mat asked on 20 Mar 2012, 10:10 PM
I'm sending CRUD actions using JSON to a PHP backend.

Reading (including filtering and paging) is working fine. I'm now at the point of Creating new records using the 'batch' option.

I've managed to get the date to save the entered data into the MySQL database, but I can't figure out what the server response should be. I've tried to respond with 1 or true on success -- but the grid doesn't recognise that the data has been saved.

I've not tried Update, but I suspect the result will be the same.

What am I missing?

Thanks

14 Answers, 1 is accepted

Sort by
0
Mat
Top achievements
Rank 1
answered on 21 Mar 2012, 09:42 PM
Sorry for bringing this up again, but I've been going round in circles with this.

Can somebody please confirm what the expected server response is for CRUD operations (using either JSON or JSONP).

All my operations are working, but the grid doesn't reflect that until the page is reloaded.

Thanks.
0
Accepted
Alexander Valchev
Telerik team
answered on 22 Mar 2012, 04:04 PM
Hi Mat,

At Telerik's github page you can find an example showing KendoUI integration with PHP. Please also check this tutorial as it explains in details how to implement CRUD data operations.

Greetings,
Alexander Valchev
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Mat
Top achievements
Rank 1
answered on 23 Mar 2012, 07:00 PM
Hi, Thanks for the pointers, but it turns out they are pretty useless.

The PHP examples on github don't have anything for CRUD with a grid and the tutorial doesn't provide the answer.

I stumbled across the answer by accident and it would not have taken much to point it out. The server response the grid expects on a successful update is 'null' -- as simple as that. Not '1', 'true' or anything else, just null.

Hope this is useful if anyone else is struggling (maybe it's just me).

Cheers.

0
Noneof
Top achievements
Rank 1
answered on 20 Apr 2012, 10:40 AM
Many thanks Mat  (: 
0
Mat
Top achievements
Rank 1
answered on 20 Apr 2012, 01:14 PM
No problem.

I'm beginning to really like this framework and I'm seriously considering replacing all my existing jQuery DataTables with Kendo Grid and other JQuery UI widgets with Kendo alternatives. The only thing I'm wary about is the documentation, which can be sparse.

There are probably relatively simple ways to achieve everything I need, but it takes a lot of digging around for examples.
0
jules
Top achievements
Rank 1
answered on 31 Aug 2012, 09:37 AM
Hi,

I have problems with the example's tutorial. Create doesn't work. Is it possible to have files which working for a comparaison with mine ? I don't understand why it doesn't work.
Mat, can you send me your crud files example please ?
Sorry for my bad english
0
Nohinn
Top achievements
Rank 1
answered on 31 Aug 2012, 10:29 AM
A very simple case of a php kendo grid with crud

Index:
<html>
<head>
<title>GRID PHP</title>
<script type="text/javascript" src="http://cdn.kendostatic.com/2012.2.710/js/jquery.min.js"></script>
<script type="text/javascript" src="http://cdn.kendostatic.com/2012.2.710/js/kendo.all.min.js"></script>
<script type="text/javascript">
$(function() {
   $("#grid").kendoGrid({
                        dataSource: {
                            transport: {
                                read: "data/fetch.php",
                                            update: {url:"data/update.php", type:"POST"},
                                            create: {url:"data/create.php",type:"POST"},
                                            destroy: {url:"data/destroy.php",type:"POST"}
                            },
                                       batch: true,
                            schema: {
                                model: {
                                                 id: "Id",
                                    fields: {
                                        Id: { type: "number" },
                                        Column1: { type: "string"},
                                        Column2: { type: "string" },
                                    }
                                }
                            },
                            pageSize: 25
                        },
                                 editable:  "inline",
                        height: 600,
                        filterable: true,
                        sortable: true,
                        pageable: true,
                                 toolbar: ["create"],
                        columns: [{
                                field:"Column1",
                                            title: "Column1",
                                filterable: true
                            },
                            {
                                field: "Column2",
                                title: "Column2",
                            },
                                      { command: [{text:"Edit record", name:"edit"}, {text:"Delete record",name:"destroy"}], title: " ", width: "210px" }
                        ]
                    });
});
</script>
</head>
<body>
<div id="grid"></div>
</body>
</html>    

The data part.
Fetch.php:
<?php
header("Content-type: application/json");
$con = mysql_connect("host", "user", "password");
if (!$con)
{
  die('Could not connect: ' . mysql_error());
} else {
   mysql_select_db("database", $con);
 
   $res = mysql_query("SELECT * From Table");
 
   $arr = array();
   while($obj = mysql_fetch_array($res)) {
      $arr[] = array(
         'Id' => $obj["Id"],
         'Column1' => str_replace("\'", "'", $obj["Column1"]),
         'Column2' => str_replace("\'", "'", $obj["Column2"])
      );
}
 
mysql_close($con);
 
print json_encode($arr);
}
?>     

Update.php:
<?php
   header("Content-type: application/json");
   $con = mysql_connect("host", "user", "password");
   if (!$con)
   {
     die('Could not connect: ' . mysql_error());
    } else {
     mysql_select_db("database", $con);
 
     $rs = mysql_query("UPDATE Table SET Column1 = '" . mysql_real_escape_string($_POST['models'][0]['Column1'], $con) . "', Column2 = '" . mysql_real_escape_string($_POST['models'][0]['Column2'], $con) . "' WHERE Id = " . $_POST['models'][0]['Id']);
     if ($rs) {
        echo json_encode($rs);
    }
    else {
        header("HTTP/1.1 500 Internal Server Error");
        echo "Failed on update: " . $_POST['models'][0]['Column1'];
    }
mysql_close($con);
}
?> 

Create.php:
<?php
header("Content-type: application/json");
$con = mysql_connect("host", "user", "password");
if (!$con)
{
  die('Could not connect: ' . mysql_error());
} else {
   mysql_select_db("database", $con);
 
   $rs = mysql_query("INSERT INTO Table(Column1, Column2) VALUES('" . mysql_real_escape_string($_POST['models'][0]['Column1'], $con) . "', '" . mysql_real_escape_string($_POST['models'][0]['Column2'], $con) . "')");
 
   if ($rs) {
    echo json_encode($rs);
 }
  else {
        header("HTTP/1.1 500 Internal Server Error");
        echo "Failed on insert: " . $_POST['models'][0]['Column1'];
  }
mysql_close($con);
}
?> 

Destroy.php:
<?php
header("Content-type: application/json");
$con = mysql_connect("host", "user", "password");
if (!$con)
{
  die('Could not connect: ' . mysql_error());
} else {
   mysql_select_db("database", $con);
 
   $rs = mysql_query("DELETE FROM Table WHERE Id = " . $_POST['models'][0]['Id']);
   if ($rs) {
        echo json_encode($rs);
  }
  else {
        header("HTTP/1.1 500 Internal Server Error");
        echo "Failed on delete: " . $_POST['models'][0]['Column1'];
  }
   mysql_close($con);
}
?> 


0
jules
Top achievements
Rank 1
answered on 31 Aug 2012, 11:52 AM
Great, thanks a lot Nohinn. All is working !
0
Omar
Top achievements
Rank 1
answered on 09 Oct 2012, 06:19 PM
Hello, 

I'm using the example of Nohinn success with one exception.

I receive "null" when I try to use date on create or update

Date is fine when fetching from localhost
After updating and refreshing the page the date column says only "null".

See screenshots!

Any idea of how to fix this?!
0
Kenny
Top achievements
Rank 1
answered on 13 Oct 2012, 11:30 PM
HI,

Im working with Kendo UI Grid , PHP and MySql.

I have a grid with remote data from php and mysql in json format, the data is displaying correctly 
but the paging (Client / local) is not working well  ... it shows the data paged but says "No items to display" and "0" pages,
if i chage the "items per page" it allows navigate and shows the total of pages, and if i refresh the grid  turn back to the initial state of error. What can i do for make the client paging works with this configuration (php remote data)?
thank you for helping me

 here is the code:
i attach the files php, js, sql
//Grid Code
 
$("#gridProductos").kendoGrid({/*Grid de productos y su configuracion*/
 
                    dataSource : {
                        type : "json",
                        transport : {
                            read : "data/products/products.php",
                            dataType : "json",
                            type : "POST"
                        },
                        schema : {
                            model : {
                                id : "codigo"
                            },
                            data : "dsProductos"
                        },
                        pageSize : 5
                    },
                    columns : [{
                        title : "Codigo",
                        field : "codigo"
                    }, {
                        title : "Referencia",
                        field : "referencia"
                    }, {
                        title : "Marca",
                        field : "marca"
                    }, {
                        command : "edit",
                        title : " "
                    }, {
                        command : "destroy",
                        title : " "
                    }],
                    navigatable : true,
                    selectable : "row",
                    sortable : true,
                    pageable : {
                        refresh : true,
                        pageSizes : true
                    }
 
                });

<?php
//The php code
 
include_once("../classes/ez_sql_core.php");
include_once("../classes/ez_sql_mysql.php");
include_once("../config.php");
 
$db = new ezSQL_mysql($db_user,$db_password,$db_name,$db_host);
$q="SELECT `codigo`, `referencia`, `marca` FROM `productos`";
$productos=$db->get_results($q,ARRAY_A);
header("Content-type: application/json");
echo "{\"dsProductos\":" .json_encode($productos). "}";
?>


//The json data returned
{"dsProductos":[{"codigo":"10","referencia":"Sal","marca":"Refisal"},{"codigo":"11","referencia":"Arroz Blanco","marca":"Diana"},{"codigo":"12","referencia":"Aceite Canola","marca":"Fino"},{"codigo":"13","referencia":"Panela Palestina","marca":"La Palestina"},{"codigo":"14","referencia":"Jabon Lavaplatos Liq","marca":"Axion"},{"codigo":"15","referencia":"Azucar 1lb","marca":"Manuelita"},{"codigo":"16","referencia":"Salsa de tomate Fruc","marca":"Fruco"},{"codigo":"17","referencia":"Mayonesa Fruco","marca":"Fruco"},{"codigo":"18","referencia":"Papas fritas Margari","marca":"Margarita"},{"codigo":"19","referencia":"Papas fritas Margari","marca":"Margarita"},{"codigo":"20","referencia":"Caja Gelatina Gelada","marca":"Gelada"}]}


0
dejla
Top achievements
Rank 1
answered on 06 May 2013, 03:45 AM
Hello !
Thx Nohinn for  for your code!
I could not make the connection with my database
Please someone can  help me !
0
dejla
Top achievements
Rank 1
answered on 07 May 2013, 03:42 AM
Hello !
Thx Nohinn for  for your code! 
I could not make the connection with my database
Please someone can  help me !
0
Kajal
Top achievements
Rank 1
answered on 13 Apr 2016, 07:15 PM

Hi,

I am getting 500 internal server error for update and destroy functionalities.Please help:(:(

 

0
Dimo
Telerik team
answered on 19 Apr 2016, 07:19 AM
Hi Kajal,

Internal server error indicates a server-side exception. Please debug the server-side code and see what the exact exception message is. This can reveal the actual cause of the problem.

In case you need further assistance, please open a separate forum thread or support ticket, and provide enough information and code for us to pinpoint what needs to be changed in the implementation.

Regards,
Dimo
Telerik
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
Tags
Grid
Asked by
Mat
Top achievements
Rank 1
Answers by
Mat
Top achievements
Rank 1
Alexander Valchev
Telerik team
Noneof
Top achievements
Rank 1
jules
Top achievements
Rank 1
Nohinn
Top achievements
Rank 1
Omar
Top achievements
Rank 1
Kenny
Top achievements
Rank 1
dejla
Top achievements
Rank 1
Kajal
Top achievements
Rank 1
Dimo
Telerik team
Share this question
or