0

I'm trying to learn jqGrid and i have some annoying problem which i can't figure it out. I'm using Navigation Grid for add, edit and deleting data. The problem is, i can add,edit or delete datas from the web. But the changes won't be applied for my server. So for example if i edit the first row it changes in my Nav Grid but then if i reload the page, its there again.

NavGridHTML.html

<!DOCTYPE html>
    <html>
    <head>
        <title>Navigation Grid</title>          
        <link rel="stylesheet" type="text/css" media="screen" href="css/jquery-ui.css" />
        <link rel="stylesheet" type="text/css" media="screen" href="css/ui.jqgrid.css" />

        <style type="text/css">
        html, body {
            margin: 0;
            padding: 0;
            font-size: 75%;
        }
        </style>

        <script src="js/jquery-1.11.0.min.js" type="text/javascript"></script>
        <script src="js/jquery.jqGrid.min.js" type="text/javascript"></script>
        <script src="js/i18n/grid.locale-en.js" type="text/javascript"></script>

        <script type="text/javascript">
            $(function () {

                jQuery("#navgrid").jqGrid({

                    url: "GridPHP.php",
                    editurl:"GridEditPHP.php",
                    datatype: "json",
                    mtype: "GET",
                    //cellEdit: true,
                    //cellsubmit: "remote",
                    //cellurl: "GridEditPHP.php",
                    colNames: ["Film ID","Title","Description","Length","Rating"],
                    colModel: [
                        { name: "film_id", index: "film_id", width: 40, editable: false, editoptions: {readonly:true,size: 10}},
                        { name: "title", index: "title", width: 180, editable:true, editoptions: {size: 10}},           
                        { name: "description", index: "description", width: 700, sortable: false, editable:true, edittype: "textarea", editoptions: {rows:"3", cols:"30"}},
                        { name: "length", index: "length", width: 50, align: "right", editable:true, editoptions: {size: 10}},
                        { name: "rating", index: "rating", width: 70, align: "right", editable:true, editoptions: {size: 10}}
                    ],
                    pager: "#pagernav",
                    rowNum: 10,
                    rowList: [10,20,30],
                    sortname: "film_id",
                    sortorder: "asc",
                    viewrecords: true,
                    //gridview: true,
                    //autoencode: true,
                    caption: "Navigation Table"
                    //  height: 210

                });

                jQuery("#navgrid").jqGrid('navGrid','#pagernav',
                {edit:true,add:true,del:true, search: false, view: true}, // options
                {height: 280, reloadAfterSubmit:false, recreateForm: true, closeAfterEdit: true, editCaption: "The Edit Dialog", saveData: "Data has been changed! Save changes?", closeOnEscape: true}, // edit options
                {height: 280, reloadAfterSubmit:false, recreateForm: true, closeAfterAdd: true, closeOnEscape: true}, // add options
                {reloadAfterSubmit:false, closeOnEscape: true}, // del options
                {}, // search options
                {closeOnEscape: true}
                );

            });     
        </script>

    </head>
    <body>
        <table id="navgrid"><tr><td></td></tr></table>
        <div id="pagernav"></div>
    </body>
    </html>

GridPHP.php

<?php
include("GridCONFIG.php");

$page = $_REQUEST["page"];
$limit = $_REQUEST["rows"];
$sidx = $_REQUEST["sidx"];
$sord = $_REQUEST["sord"];

if (!$sidx) $sidx = 1;

$totalrows = isset($_REQUEST["totalrows"]) ? $_REQUEST["totalrows"]: false;

if($totalrows) {

    $limit = $totalrows;

}

$db =mysqli_connect($host, $username, $password, $database) or die ("Connection Error: " . mysqli_error($db));

mysqli_select_db($db,$database) or die ("Error connecting to db!");
$result = mysqli_query($db,"SELECT COUNT(*) AS count FROM film");
$row = mysqli_fetch_array($result,MYSQLI_ASSOC);
$count = $row['count'];

if ($count > 0 ) {

    $var = @($count/$limit);
    $totalpages = ceil ($var);

} else {

    $totalpages = 0;

}

if ($page > $totalpages) $page=$totalpages;

if ($limit < 0) $limit = 0;

$start = $limit*$page - $limit;
if ($start < 0) $start = 0;

$sql = "SELECT film_id, title, description, length, rating FROM film ORDER BY $sidx $sord LIMIT $start , $limit";
$result = mysqli_query($db,$sql) or die ("Couldn't execute query! ".mysqli_error($db));

$responce = new \stdClass();
$responce -> success = false;
$responce -> page = $page;
$responce -> total = $totalpages;
$responce -> records = $count;

$i = 0;
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {

    $responce -> rows[$i]["id"] = $row["film_id"];
    $responce -> rows[$i]['cell'] = array($row["film_id"],$row["title"],$row["description"],$row["length"],$row["rating"]);
    $i++;

}

echo json_encode($responce);
mysqli_close($db);

?>

GridEditPHP.php

<?php
include("GridCONFIG.php");

$db =mysqli_connect($host, $username, $password, $database) or die ("Connection Error: " . mysqli_error($db));
mysqli_select_db($db,$database) or die ("Error connecting to db.");

$filmid = $_POST["film_id"];
$title = $_POST["title"];
$description = $_POST["description"];
$length = $_POST["length"];
$rating = $_POST["rating"];

//switch($_REQUEST["oper"]) {
//
//  case "add":
//      $sql = "INSERT INTO film (film_id,title,description,length,rating) VALUES ($filmid,$title,$description,$length,$rating)";
//      mysqli_query($db, $sql);
//  break;
//  
//  case "edit":
//      $sql = "UPDATE film SET film_id=$filmid, title=$title, description=$description, length=$length, rating=$rating";
//      mysqli_query($db, $sql);
//  break;
//  
//  case "del":
//      $sql = "DELETE FROM film";
//      mysqli_query($db, $sql);
//  break;
echo "TEST!!!";
if($_REQUEST["oper"]=='add') {

    $sql = "INSERT INTO film (film_id,title,description,length,rating) VALUES ($filmid,$title,$description,$length,$rating)";
    if(mysqli_query($db, $sql)) {

        echo "Film added.";

    } else {

        echo "Error adding film: " .mysqli_error($db);

    }

} elseif($_REQUEST["oper"]=='edit') {

    $sql = "UPDATE film SET title=$title, description=$description, length=$length, rating=$rating WHERE film_id=$filmid";
    if(mysqli_query($db, $sql)) {

        echo "Film edited.";

    } else {

        echo "Error editing film: " .mysqli_error($db);

    }

} elseif($_POST["oper"]=='del') {

    $sql = "DELETE FROM film WHERE film_id=$filmid";
    if(mysqli_query($db, $sql)) {

        echo "Film deleted.";

    } else {

        echo "Error deleting film: " .mysqli_error($db);

    }

}

mysqli_close($db);

?>

I would be appreciated if someone can solve this annoying problem... And just for info, i already read Documentation, and looked every single example or solution on the internet. But still couldn't find any solution.

And one additional question: How can i see the output from GridEditPHP.php echo "TEST!!!";... I mean, where can i see it on my website?

The Database is sample database called "sakila".

  • I'm not a PHP developer, but I'd recommend you to add the lines `header('Cache-Control: private, max-age=0');` and `header('Content-type: application/json');``before the line `echo json_encode($responce);` in `GridPHP.php`. It's important that the first/old response for filling the grid will be not cached in wed browser. The next problem: you use `film_id` column in jqGrid, which you use in `GridEditPHP.php`, but which will be **not send** to the server. If you really want to show the column to the users then you can add the option `prmNames: { id: "film_id" }` to jqGrid options. – Oleg Apr 13 '18 at 08:59
  • @Oleg Firstly thank you for your comment. I hoped that you answer my question because i see your answers everywhere for jqGrid topics. I added now `header("Cache-Control: private, max-age=0") header("Content-type: application/json"); $page = $_REQUEST["page"]; $limit = $_REQUEST["rows"];` in GridPHP.php. But after that i can't see any result on my Nav Grid Table. Additionally i fixed this line as you said `{edit:true,add:true,del:true, search: false, view: true, prmNames: {id: "film_id"}}, // options` – BatuhanSertoglu Apr 13 '18 at 09:22
  • `prmNames: {id: "film_id"}` is **jqGrid option** and not an option of `navGrid`. You placed it on the wrong place currently. Move it after `rowList: [10,20,30],` for example. About initial filling the grid: I recommend you to use Network tab of Developer Tools of Chrome/IE/Firefox to see the response from the server inclusive HTTP headers. Additional question: which **version** of jqGrid you use and from which **fork** ([free jqGrid](https://github.com/free-jqgrid/jqGrid), commercial [Guriddo jqGrid JS](http://guriddo.net/?page_id=103334) or an old jqGrid in version <=4.7)? – Oleg Apr 13 '18 at 09:28
  • @Oleg Oh thank you for your recommend..Network tab was just the thing what i'm looking for..The version is 3.6, at least its the version what in jqGrid/js/Changes.txt says..i'm not sure which fork it is, i don't know exactly how to find that out but i think it was Guriddo. – BatuhanSertoglu Apr 13 '18 at 09:46
  • I develop [free jqGrid](https://github.com/free-jqgrid/jqGrid) fork, which is upwards compatible with old jqGrid 4.x versions. You can load it from CDN like it's described [here](https://github.com/free-jqgrid/jqGrid/wiki/Access-free-jqGrid-from-different-CDNs). See examples [here](https://free-jqgrid.github.io/getting-started/index.html). If you open the file `jquery.jqgrid.min.js` in text editor you will see the name of the product and the version inside of the comment lines with which the file is started. You can use [Fiddler](https://www.telerik.com/fiddler) tool instead of Developer Tools – Oleg Apr 13 '18 at 09:51
  • My jqGrid version is 5.3.1 and the fork is Guriddo jqGrid JS.. I finally figure it out my problem. Network Tab was the solution actually, it helped for debugging. It was a simple Syntax Error. I will write the answer – BatuhanSertoglu Apr 13 '18 at 10:04
  • 5.3.1 is **commercial** version available for the prices seen [here](http://guriddo.net/?page_id=103334). You can see the link to the pabe in the starting comment of `jquery.jqgrid.min.js` file. I have no relation to Guriddo. I develop *alternative* fork, which can be used for free in difference to Guriddo. It's your choice (or the choice of your company), which fork to use. – Oleg Apr 13 '18 at 10:10
  • It's already set by my company which fork that we use so i can't change but thanks anyway. – BatuhanSertoglu Apr 13 '18 at 10:23
  • You are welcome! By the way, free jqGrid display by default the error messages which you could localize in HTTP traffic during loading the grid. If you use the fork, which don't do that, it's strictly recommended to add `loadError` to all you jqGrids. See [the old answer](https://stackoverflow.com/a/6969114/315935) for more details. I recommend you to modify your server code temporary to have the same old syntax error and to verify whether the corresponding error message will be displayed by `loadError`. – Oleg Apr 13 '18 at 10:30

1 Answers1

0

In GridEditPHP.php, i had a Syntax Error.

elseif($_REQUEST["oper"]=='edit') {

    $sql = "UPDATE film SET title=$title, description=$description, length=$length, rating=$rating WHERE film_id=$filmid";
    if(mysqli_query($db, $sql)) {

        echo "Film edited.";

    } else {

        echo "Error editing film: " .mysqli_error($db);

    }

$sql should to be set like that:

$sql = "UPDATE film SET title='$title', description='$description', length=$length, rating='$rating' WHERE film_id=$filmid";

Now everything is working fine..I'm able to add, edit or delete any data from server by my web server.

Additionally thanks to @Oleg, i figured this out with his suggestion. See Network Tab - Developer Tools or Fiddler