jQWidgets Forums
jQuery UI Widgets › Forums › Grid › jqxGrid/json data – can't figure out what's wrong
Tagged: jqxgrid mysql php
This topic contains 1 reply, has 2 voices, and was last updated by bachkylan 10 years, 4 months ago.
-
Author
-
I have copied a json/mysql jqxgrid sample and modified to try to work with my data and I’m stumped why I’m not seeing anything.
In my sample.php I performed a test to require data.php. The result was that i see json data returned, so data.php is correct.
However, when I try run my simple two field example I do not get anything returned. On the other hand I have successfully run the original sample.php from the jqxgrid help and it works just fine … so I know that i have the correct list of jqx objects.
Can someone take a look at my setup code and I identify what I’m missing? For the source: on the jqxgrid, I have tried just the source variable and the dataAdapter variable and both don’t show anything. I’ve spent about 6 hours trying various things without success.
I sure could use some help!
data.php
<?php $mysqlserver="localhost"; $mysqlusername="root"; $mysqlpassword="pwd"; $mysqldatabase="test"; $link = mysql_connect($mysqlserver, $mysqlusername, $mysqlpassword); if (!$link) { die ("<a>Error: (" . mysql_errno() . "): " . mysql_error() . "</a>"); } if ($link) { $db_selected = mysql_select_db($mysqldatabase, $link); if (!$db_selected) { mysql_close($link); $link = ''; echo '<a> Can\'t use database: ' . $mysqldatabase . '</a> '; die ("<a>Error: (" . mysql_errno() . "): " . mysql_error() . "</a>"); } } if(!$link) { die(mysql_error()); } else { // get data and store in a json array $query = "Select Field1, Field2 From importdata"; if (isset($_GET['update'])) { //// UPDATE COMMAND echo true; } else { // Including where clause $sWhere = ""; // Append where conditions if ($sWhere) { $query = $query . ' Where ' . $sWhere ; } else { $query = $query . " Limit 0,10" ; } // SELECT COMMAND $result = mysql_query($query) or die("SQL Error 1: " . mysql_error()); while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { $employees[] = array( 'Field1' => $row['Field1'], 'Field2' => $row['Field2'] ); } echo json_encode($employees); } mysql_close($link); } ?>
sample.php
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>PHP JQXGRID JSON - Example</title> <link rel="stylesheet" href="Scripts/jqwidgets-ver3.6.0/jqwidgets/styles/jqx.base.css" type="text/css" /> <script type="text/javascript" src="Scripts/jqwidgets-ver3.6.0/scripts/jquery-1.11.1.min.js"></script> <script type="text/javascript" src="Scripts/jqwidgets-ver3.6.0/jqwidgets/jqxcore.js"></script> <script type="text/javascript" src="Scripts/jqwidgets-ver3.6.0/jqwidgets/jqxdata.js"></script> <script type="text/javascript" src="Scripts/jqwidgets-ver3.6.0/jqwidgets/jqxbuttons.js"></script> <script type="text/javascript" src="Scripts/jqwidgets-ver3.6.0/jqwidgets/jqxscrollbar.js"></script> <script type="text/javascript" src="Scripts/jqwidgets-ver3.6.0/jqwidgets/jqxmenu.js"></script> <script type="text/javascript" src="Scripts/jqwidgets-ver3.6.0/jqwidgets/jqxgrid.js"></script> <script type="text/javascript" src="Scripts/jqwidgets-ver3.6.0/jqwidgets/jqxgrid.selection.js"></script> <script type="text/javascript"> $(document).ready(function () { // initialize jqxGrid var theme = "Summer"; var dtasource = { datatype: "json", datafields: [{ name: 'Id' }, { name: 'BikeNo' }], url: 'data.php' }; var dataAdapter = new $.jqx.dataAdapter(source, { autoBind: true, async: false, downloadComplete: function () { alert("downloadComplete") }, loadComplete: function () { alert("loadComplete") }, loadError: function () { alert("loadError") } }); $("#jqxgrid").jqxGrid({ width: 1700, height: 600, selectionmode: 'singlerow', source: dataAdapter, theme: theme, editable: true, columns: [ { text: 'Field1', editable: false, datafield: 'Field1', width: 100 }, { text: 'Field2', editable: false, datafield: 'Field2', width: 100 } ] }); }); </script> </head> <body class='default'> <h1>PHP JQXGRID JSON - Example</h1> <div id='jqxWidget' style="font-size: 13px; font-family: Verdana; float: left;"> <div id="jqxgrid"> </div> </div> </body> </html>
I think it can help you, pls try!
PHP : nhap_nhanvien.php
<?php
#Include the connect.php file
include(‘connect.php’);
#Connect to the database
//connection String
$connect = mysql_connect($db_host, $db_username, $db_password)
or die(‘Could not connect: ‘ . mysql_error());
//Select The database
$bool = mysql_select_db($db_name, $connect);
if ($bool === False){
print “can’t find $db_name”;
}if (isset($_GET[‘insert’])){
// INSERT COMMAND
$insert_query = “INSERT INTOnhanvien
(IdNV
,MaNV
,TenNV
,DienThoai
) VALUES (‘”.$_GET[‘idnv’].”‘,'”.$_GET[‘manv’].”‘,'”.$_GET[‘tennv’].”‘,'”.$_GET[‘dienthoai’].”‘)”;
$result = mysql_query($insert_query) or die(“SQL Error 1: ” . mysql_error());
echo $result;
}else if (isset($_GET[‘update’])){
// UPDATE COMMAND
$update_query = “UPDATEnhanvien
SETIDNV
='”.$_GET[‘idnv’].”‘,
MaNV
='”.$_GET[‘manv’].”‘,
TenNV
='”.$_GET[‘tennv’].”‘,
DienThoai
='”.$_GET[‘dienthoai’].”‘WHEREIDNV
='”.$_GET[‘idnv’].”‘”;
$result = mysql_query($update_query) or die(“SQL Error 1: ” . mysql_error());
echo $result;
}
else if (isset($_GET[‘delete’])){
// DELETE COMMAND
$delete_query = “DELETE FROMnhanvien
WHEREIDNV
='”.$_GET[‘idnv’].”‘”;
$result = mysql_query($delete_query) or die(“SQL Error 1: ” . mysql_error());
echo $result;
}else{
// SELECT COMMAND
$result = mysql_query($query) or die(“SQL Error 1: ” . mysql_error());
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$khachhang[] = array(
‘IDNV’ => $row[‘idnv’],
‘MaNV’ => $row[‘manv’],
‘TenNV’ => $row[‘tennv’],
‘DienThoai’ => $row[‘dienthoai’]
);
}
echo json_encode($khachhang);
}
?>HTML :
var source =
{
datatype: “json”,
datafields: [
{ name: ‘idnv’ },
{ name: ‘manv’ },
{ name: ‘tennv’ },
{ name: ‘dienthoai’ }
],
id: ‘idnv’,
url: ‘php/get_data_nhan_vien.php’,
cache: false,
addrow: function (rowid, rowdata, position, commit) {
var data = “insert=true&” + $.param(rowdata);
$.ajax({
dataType: ‘json’,
url: ‘php/nhap_nhanvien.php’,
data: data,
cache: false,
success: function (data, status, xhr) {
commit(true);
},
error: function (jqXHR, textStatus, errorThrown) {
commit(false);
}
});
},
deleterow: function (rowid, commit) {
// synchronize with the server – send delete command
var data = “delete=true&” + $.param({ idnv: rowid });
$.ajax({
dataType: ‘json’,
url: ‘php/nhap_nhanvien.php’,
cache: false,
data: data,
success: function (data, status, xhr) {
// delete command is executed.
commit(true);
},
error: function (jqXHR, textStatus, errorThrown) {
commit(false);
}
});
},
updaterow: function (rowid, rowdata, position, commit) {
// synchronize with the server – send update command
var data = “update=true&” + $.param(rowdata);
$.ajax({
dataType: ‘json’,
url: ‘php/nhap_nhanvien.php’,
cache: false,
data: data,
success: function (data, status, xhr) {
// update command is executed.
commit(true);
},
error: function (jqXHR, textStatus, errorThrown) {
commit(false);
}
});
}
}; -
AuthorPosts
You must be logged in to reply to this topic.