CRUD actions - what should server response be?

15 posts, 1 answers
  1. Mat
    Mat avatar
    13 posts
    Member since:
    Mar 2012

    Posted 20 Mar 2012 Link to this post

    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
  2. Mat
    Mat avatar
    13 posts
    Member since:
    Mar 2012

    Posted 21 Mar 2012 Link to this post

    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.
  3. Kendo UI is VS 2017 Ready
  4. Answer
    Alexander Valchev
    Admin
    Alexander Valchev avatar
    2875 posts

    Posted 22 Mar 2012 Link to this post

    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!
  5. Mat
    Mat avatar
    13 posts
    Member since:
    Mar 2012

    Posted 23 Mar 2012 Link to this post

    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.

  6. Noneof
    Noneof avatar
    1 posts
    Member since:
    Apr 2012

    Posted 20 Apr 2012 Link to this post

    Many thanks Mat  (: 
  7. Mat
    Mat avatar
    13 posts
    Member since:
    Mar 2012

    Posted 20 Apr 2012 Link to this post

    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.
  8. jules
    jules avatar
    5 posts
    Member since:
    Aug 2012

    Posted 31 Aug 2012 Link to this post

    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
  9. Nohinn
    Nohinn avatar
    167 posts
    Member since:
    Feb 2011

    Posted 31 Aug 2012 Link to this post

    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);
    }
    ?> 


  10. jules
    jules avatar
    5 posts
    Member since:
    Aug 2012

    Posted 31 Aug 2012 Link to this post

    Great, thanks a lot Nohinn. All is working !
  11. Omar
    Omar avatar
    8 posts
    Member since:
    Oct 2012

    Posted 09 Oct 2012 Link to this post

    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?!
  12. Kenny
    Kenny avatar
    1 posts
    Member since:
    Oct 2012

    Posted 13 Oct 2012 Link to this post

    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"}]}


  13. dejla
    dejla avatar
    3 posts
    Member since:
    Apr 2013

    Posted 05 May 2013 Link to this post

    Hello !
    Thx Nohinn for  for your code!
    I could not make the connection with my database
    Please someone can  help me !
  14. dejla
    dejla avatar
    3 posts
    Member since:
    Apr 2013

    Posted 06 May 2013 Link to this post

    Hello !
    Thx Nohinn for  for your code! 
    I could not make the connection with my database
    Please someone can  help me !
  15. Kajal
    Kajal avatar
    1 posts
    Member since:
    Apr 2016

    Posted 13 Apr in reply to Nohinn Link to this post

    Hi,

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

     

  16. Dimo
    Admin
    Dimo avatar
    8332 posts

    Posted 19 Apr Link to this post

    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!
Back to Top
Kendo UI is VS 2017 Ready