如何从jqgrid中的添加对话框向MySQL数据库添加数据 [英] How to add data to mysql database from add dialog in jqgrid
本文介绍了如何从jqgrid中的添加对话框向MySQL数据库添加数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
I am trying to have a jqgrid on my webpage with all the functionality. Currently I am able to fill the grid with data from mysql database. Now I have kept add:true in the navgrid option and with the '+' icon it opens a add dialog box with 3 fields which on submit I need to add to mysql database. Here's my jqgrid code:
<pre lang="Javascript">
<script type="text/javascript">
jQuery(document).ready(function(){
$("#grid").jqGrid({
// data: mydata,
datatype: 'json',
mtype: 'POST',
url: 'showResources.php',
width: 700,
colNames:["LAB NAME","RESOURCE NAME","DESCRIPTION"],
colModel:[
{name:'lab_name', index:'lab_name', width:40,editable:true, edittype: 'select', editoptions:{value:"nccs:NCCS;tcs:TCS;iisc:IISC;icgeb:ICGEB", dataInit: function(elem)
{
$(elem).width('auto');
}
}},
{name:'resource_name', index:'resource_name', width:50,editable:true, editoptions:{size:'auto'}},
{name:'description', index:'description', width:100,editable:true, edittype: 'textarea', editoptions:{rows:"2",cols:"20"}},
],
pager: "#pager",
rowNum:10,
rownumbers: true,
rowList:[10,20,30],
gridview: true,
sortname: 'id',
viewrecords: true,
loadonce: true,
sortorder: "desc",
caption:"Resources",
height: 250,
editurl: 'addResource.php'
}).navGrid('#pager',{edit:false,add:true,del:false,search:true},{width: '330', closeAfterAdd: true });
});
</script>
</pre>
Here url:showResource.php is the page where I have the logic to fetch data into grid from mysql, and editurl: 'addResource.php' is where my logic for adding the row is which is:
code:
<pre lang="PHP">$lab_name = $_POST['lab_name']; //lab_name field from POST above
$resource_name = $_POST['resource_name']; //resource_name field from POST above
$description = $_POST['description']; //description field from POST above
$tb_name = 'tb_systb_resources';
$add_query = "INSERT INTO $tb_name ('','lab_name','resource_name','description') VALUES ('',$lab_name,$resource_name,$description)";
$result = mysql_query($add_query);
if($result)
{
echo("<br>Input data is succeed");
}
else
{
echo("<br>Input data is fail");
}</pre>
Now when the page is loaded it says
Notice: Undefined index: lab_name in /opt/lampp/htdocs/hemang/addResource.php on line 10
Notice: Undefined index: resource_name in /opt/lampp/htdocs/hemang/addResource.php on line 11
Notice: Undefined index: description in /opt/lampp/htdocs/hemang/addResource.php on line 12
which i have printed and the grid is filled, and the data does add from the dialog but does not store to db. So when I refresh the page or grid it disappears. I am not sure of how to pass the 3 fields' values from the submit button in add dialog box.
Please help.
推荐答案
(#grid").jqGrid({ //数据:mydata, 数据类型:"json", mtype:"POST", 网址:"showResources.php", 宽度:700, colNames:[实验室名称",资源名称",说明"], colModel:[ {name:'lab_name',索引:'lab_name',宽度:40,可true,edittype:'select',editoptions:{value:"nccs:NCCS; tcs:TCS; iisc:IISC; icgeb:ICGEB", dataInit:函数(元素) {
("#grid").jqGrid({ // data: mydata, datatype: 'json', mtype: 'POST', url: 'showResources.php', width: 700, colNames:["LAB NAME","RESOURCE NAME","DESCRIPTION"], colModel:[ {name:'lab_name', index:'lab_name', width:40,editable:true, edittype: 'select', editoptions:{value:"nccs:NCCS;tcs:TCS;iisc:IISC;icgeb:ICGEB", dataInit: function(elem) {
(elem).width('auto'); } }}, {name:'resource_name',索引:'resource_name',宽度:50,可true,editoptions:{size:'auto'}}, {name:'description',index:'description',width:100,editable:true,edittype:'textarea',editoptions:{rows:"2",cols:"20"}}}, ], 传呼机:#pager", rowNum:10, 行号:true, rowList:[10,20,30], gridview:是的, sortname:'id', 观看记录:是的, loadonce:是的, sortorder:"desc", 标题:资源", 高度:250, editurl:'addResource.php' }).navGrid('#pager',{edit:false,add:true,del:false,search:true},{width:'330',closeAfterAdd:true});; }); & lt;/script> < /pre > 在这里url:showResource.php是我可以从mysql将数据提取到网格中的逻辑的页面,在editurl中:'addResource.php'是我添加行的逻辑的地方是: 代码: < pre =" PHP"
(elem).width('auto'); } }}, {name:'resource_name', index:'resource_name', width:50,editable:true, editoptions:{size:'auto'}}, {name:'description', index:'description', width:100,editable:true, edittype: 'textarea', editoptions:{rows:"2",cols:"20"}}, ], pager: "#pager", rowNum:10, rownumbers: true, rowList:[10,20,30], gridview: true, sortname: 'id', viewrecords: true, loadonce: true, sortorder: "desc", caption:"Resources", height: 250, editurl: 'addResource.php' }).navGrid('#pager',{edit:false,add:true,del:false,search:true},{width: '330', closeAfterAdd: true }); }); </script> </pre> Here url:showResource.php is the page where I have the logic to fetch data into grid from mysql, and editurl: 'addResource.php' is where my logic for adding the row is which is: code: <pre lang="PHP">
lab_name =
lab_name =
这篇关于如何从jqgrid中的添加对话框向MySQL数据库添加数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文