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

How to validate the data sent to the server when an UPDATE is executed?

0 Answers 140 Views
Data Source
This is a migrated thread and some comments may be shown as answers.
jiff
Top achievements
Rank 1
jiff asked on 24 Nov 2011, 02:02 AM
I'm trying to validate the data sent to the server when I run an UPDATE operation.

I'll try to be clearer. Here's the code in detail:


Kendo Script
------------------

          <script>
                $(document).ready(function() {
                    
 
                    var Item = kendo.data.Model.define({
                        id: "id_student"
                    });
                    
                    var model = null; 


      var JsonDataSource = new kendo.data.DataSource({
                        transport: {
                            read: {
                                url: "students_read.php", 
                                dataType: "JSON"
                            },
                            create: {
                                url: "students_add.php", 
                                type: "POST"
                            },
                            update: {
                                url: "students_update.php",
                                type: "POST"
                            } ,
destroy: {
                                url: "students_delete.php",
                                type: "POST"
                            }
                        },
                        schema:{
                            model: Item
                        }
                    });




                  function onChange() {
                   
    var tr = this.select();
                        var id = tr.data("id");
                        model = this.dataSource.get(id);
                   
                        $("#id").val(model.get("id_student"));         
                        $("#name").val(model.get("name"));
                                   
                    }


                 $("#grid").kendoGrid({
                        autoBind: false,
                        dataSource:JsonDataSource,
                        height: 350,
                        selectable: "row",
                        pageable: false,
                        sortable: true,
                        scrollable: true,
                        change: onChange,
                        columns: [
                                  { title : "Id", field :"id_student"},
                                  { title : "Name", field :"name"}
                                ]
                    });
                    
                    $("#btn_load").click(function() {
                        JsonDataSource.read({name: $('#search').val()});
                    });
                    
                     $("#search").keydown(function(e) {
                        if (e.keyCode === kendo.keys.ENTER) {
                            JsonDataSource.read({name: $('#search').val()});
                        }
                    });
                    
                    $("#btn_add").click(function() {
                        $('#id').val(null);
                        $('#name').val(null);
                    });
                    
                    $("#btn_save").click(function() {
                        if ($('#id').val() == ''){
                            JsonDataSource.add({ aluno: $('#name').val() });
                            JsonDataSource.sync();
                            $('#search').val($('#name').val());
                            JsonDataSource.read({name: $('#search').val()});
                        }else{
                            model.set("aluno", $('#name').val());
                            JsonDataSource.sync();
                        }
                    });
                    
                    $("#btn_delete").click(function() {
                        JsonDataSource.remove(model);
                        JsonDataSource.sync();
                    });
                
                });
            </script>


HTML tags
---------------

       <div id="example" class="k-content">
            <div>
                Search:&nbsp;&nbsp;<input id="search" name="search" />
                <button id="btn_load">Load Data</button>
            </div><br/>
            <div id="grid"></div>
            <dl>
                <dt>Student:</dt><br/>
                <dd>Id:&nbsp;&nbsp;<input id="id" name="id" readonly="readonly"/></dd><br/>
                <dd>Name:&nbsp;&nbsp;<input id="name" name="name" /></dd><br/>
                <dd><button id="btn_add">New</button>&nbsp;&nbsp;&nbsp;<button id="btn_save">Save</button>&nbsp;&nbsp;&nbsp;<button id="btn_delete">Delete</button></dd>
            </dl>
       </div>
            
PHP script (students_update.php)
--------------------------------------------
<?php


$host = "localhost";
$username = "root";
$password = "root";
$db = "school";


$id_student = $_POST["id_student"];
$name = $_POST["name"];
        
    mysql_connect($host,$username,$password);
mysql_selectdb($db);

mysql_query("UPDATE students set name = '".$name."'  WHERE id_student = '".$id_student."'");
$result = array("result" => (mysqli_affected_rows >= 0)); 

echo json_encode($result);
?>


I wonder how to implement the code above to show the result of the UPDATE operation (if there was success or failure) via an alert (). example;


if (result == true) {
  alert("Sucess Update");
}else{
  alert("Fault Update");
}


The question is: How do I do this? Anybody could implement the code? The only thing missing to complete the example that full CRUD (Kendo + PHP) and send to the community.

No answers yet. Maybe you can help?

Tags
Data Source
Asked by
jiff
Top achievements
Rank 1
Share this question
or