Forum
17:18
04/12/2014
Hello.
I am new with jqgrid and I don't know how it works. I am a PHP/MySQL/Html programmer........
My Problem is, how can I write the data to a MySQL Database?
I read the data from database and the grid show it, but when I click add/edit/del nothing is saved.
My mainfile:
<?php
require_once("models/config.php");
include("template/header.php");?>
<script type="text/javascript">
$(function () {
$("#list").jqGrid({
url: "adm_land_action.php",
datatype: "xml",
mtype: "POST",
colNames: ["id", "Aufenthaltsort"],
colModel: [
{ name: "id", width: 50, search:false, editable: false },
{ name: "land", width: 800, editable: true, search:true, editoptions:{size:25}, edittype:"text", sorttype:'string', searchoptions:{sopt:['cn','eq','bw','ew']}}
],
pager: "#pager",
rowNum: 10,
//rowList: [10, 20, 30],
sortname: "land",
sortorder: "asc",
loadonce:false,
viewrecords: true,
gridview: true,
autoencode: true,
editurl: "adm_land_edit.php",
caption: "Aufenthaltsorte"
})
jQuery("#list").jqGrid('filterToolbar',{searchOperators : true});jQuery("#list").jqGrid('navGrid','#pager',
{search:false,edit:true,add:true,del:true}, //options
{height:280,reloadAfterSubmit:true}, // edit options
{height:280,reloadAfterSubmit:true}, // add options
{reloadAfterSubmit:true}, // del options
{} // search options
);$("#add").click(function(){
jQuery("#list").jqGrid('editGridRow',"new",{height:280,reloadAfterSubmit:true})});$("#edit").click(function(){
jQuery("#list").jqGrid('editGridRow',"edit",{height:280,reloadAfterSubmit:true})});$("#del").click(function(){
jQuery("#list").jqGrid('editGridRow',"del",{height:280,reloadAfterSubmit:true})});
});
</script><div class="container">
<header>
<?php include("template/sub_header.php"); ?>
</header>
<h1>Aufenthaltsorte</h1>
<?php if(isUserLoggedIn()) {
include('template/tm_menu.php');
} else {
echo "<meta http-equiv='refresh' content='0; URL=index.php'>";
exit;
}
?>
<article class="content"><?php if(isUserLoggedIn()) {
echo "<table id='list'></table>
<div id='pager'></div>
";} ?>
</article><?php include("template/footer.php"); ?>Â
My file to read the database adm_land_action.php
<?php
//include the information needed for the connection to MySQL data base server.
// we store here username, database and password
require_once("models/config.php");// to the url parameter are added 4 parameters as described in colModel
// we should get these parameters to construct the needed query
// Since we specify in the options of the grid that we will use a GET method
// we should use the appropriate command to obtain the parameters.
// In our case this is $_GET. If we specify that we want to use post
// we should use $_POST. Maybe the better way is to use $_REQUEST, which
// contain both the GET and POST variables. For more information refer to php documentation.
// Get the requested page. By default grid sets this to 1.
$page = $_POST['page'];// get how many rows we want to have into the grid - rowNum parameter in the grid
$limit = $_POST['rows'];// get index row - i.e. user click to sort. At first time sortname parameter -
// after that the index from colModel
$sidx = $_POST['sidx'];// sorting order - at first time sortorder
$sord = $_POST['sord'];// if we not pass at first time index use the first column for the index or what you want
if(!$sidx) $sidx =1;// connect to the MySQL database server
$db = mysql_connect($db_host, $db_user, $db_pass) or die("Connection Error: " . mysql_error());// select the database
mysql_select_db($db_name) or die("Error connecting to db.");// calculate the number of rows for the query. We need this for paging the result
$result = mysql_query("SELECT COUNT(*) AS count FROM ".$db_prefix."land");
$row = mysql_fetch_array($result,MYSQL_ASSOC);
$count = $row['count'];// calculate the total pages for the query
if( $count > 0 && $limit > 0) {
$total_pages = ceil($count/$limit);
} else {
$total_pages = 0;
}// if for some reasons the requested page is greater than the total
// set the requested page to total page
if ($page > $total_pages) $page=$total_pages;// calculate the starting position of the rows
$start = $limit*$page - $limit;// if for some reasons start position is negative set it to 0
// typical case is that the user type 0 for the requested page
if($start <0) $start = 0;// the actual query for the grid data
$SQL = "SELECT id, land FROM ".$db_prefix."land ORDER BY $sidx $sord LIMIT $start , $limit";
$result = mysql_query( $SQL ) or die("Couldn't execute query.".mysql_error());// we should set the appropriate header information. Do not forget this.
header("Content-type: text/xml;charset=utf-8");$s = "<?xml version='1.0' encoding='utf-8'?>";
$s .= "<rows>";
$s .= "<page>".$page."</page>";
$s .= "<total>".$total_pages."</total>";
$s .= "<records>".$count."</records>";// be sure to put text data in CDATA
while($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
$s .= "<row id='". $row['id']."'>";
$s .= "<cell>". $row['id']."</cell>";
$s .= "<cell>". $row['land']."</cell>";
$s .= "</row>";
}
$s .= "</rows>";echo $s;
?>
And my file to write to database adm_land_adit.php
<?php
//include the information needed for the connection to MySQL data base server.
// we store here username, database and password
require_once("models/config.php");$id = $_POST['id'];Â
$land = $_POST['land'];
// connect to the MySQL database server
$db = mysql_connect($db_host, $db_user, $db_pass) or die("Connection Error: " . mysql_error());// select the database
mysql_select_db($db_name) or die("Error connecting to db.");// the actual query for the grid data
$SQL = "update ".$db_prefix."land set land=$land where id=$id";
$result = mysql_query( $SQL ) or die("Couldn't execute query.".mysql_error());?>
It have only the edit function in it.
When I click on edit, the editform pop up and when I edit the entry and click on Save, the grid reloads, the form is showing again and the data is not changed.
What is wrong?
Have I don't understand the script and the functions?
Please help a newbie
Greetz Rappi
Most Users Ever Online: 715
Currently Online:
76 Guest(s)
Currently Browsing this Page:
1 Guest(s)
Top Posters:
OlegK: 1255
markw65: 179
kobruleht: 144
phicarre: 132
YamilBracho: 124
Renso: 118
Member Stats:
Guest Posters: 447
Members: 11373
Moderators: 2
Admins: 1
Forum Stats:
Groups: 1
Forums: 8
Topics: 10592
Posts: 31289
Newest Members:
, razia, Prankie, psky, praveen neelam, greg.valainis@pa-tech.comModerators: tony: 7721, Rumen[Trirand]: 81
Administrators: admin: 66