多数PHP程序都使用HTML表单从用户那里获取数据并计算结果。
首先创造一个基本的HTML大纲,包含表单控件;然后将控件进行合并(HTML表单必须包括一个提交按钮,用户单击它可以将表单数据发送到服务器。)一个单独的HTML页面可以包含多个表单。
包含表单的HTML结构和和普通的HTML结构一样。
<HTML>
<HEAD>
<TITLE>标题放在这</TITLE>
</HEAD>
<BODY>
表单页面放在这
</BODY>
</HTML>
在包含表单的HTML页面中可以使用任何HTML标签。基本的表单使用FROM标签来说明。该标签中METHOD属性接收GET或POST两个值中的一个。ACTION属性子明PHP脚本的url,该脚本可以收集通过表单收集的数据,可以是绝对路径或者相对路径。
<FORM METHOD="method" ACTION="url">
中间可以放置表单控件
</FORM>
两个常用的基本控件:文本框和提交按钮。
文本框:允许用户键入信息以发送给PHP脚本。NAME属性为文本提供名称,PHP脚本可以通过名称准确访问其内容,因此它应该是唯一的且符合PHP变量命名规则(但不需要$符号),单标签。VALUE属性指明出现在提交按钮上面的标题。创建方式如下:
<INPUT TYPE = "TEXT" NAME="text">
提交按钮:允许用户将一个表单的内容发送到服务器,一个HTML表单对应应该有一个提交按钮。
示例:一个完整的HTML表单。
<HTML>
<HEAD>
<TITLE>标题</TITLE>
</HEAD>
<BODY>
<FORM METHOD="POST" ACTION="phpinfo.php">
<INPUT TYPE="TEXT" NAME="user_name">
<BR/>
<BR/>
<INPUT TYPE="TEXT" NAME="user_email">
<BR/>
<BR/>
<INPUT TYPE="SUBMIT" VALUE="Send the Data">
</FORM>
</BODY>
</HTML>
可以在一个HTML页面中包含多个表单,注意下一个表单的FORM开始之前需要结束前一个FORM表单。
<HTML>
<HEAD>
<TITLE>标题</TITLE>
</HEAD>
<BODY>
<FORM METHOD="POST" ACTION="phpinfo.php">
<INPUT TYPE="TEXT" NAME="user_name">
<BR/>
<BR/>
<INPUT TYPE="TEXT" NAME="user_email">
<BR/>
<BR/>
<INPUT TYPE="SUBMIT" VALUE="Send the Data">
<BR/>
<BR/>
</FORM>
<FORM METHOD="POST" NAME="phpinfo.php">
<INPUT TYPE="TEXT" NAME="user_name1">
<BR/>
<BR/>
<INPUT TYPE="TEXT" NAME="user_email1">
<BR/>
<BR/>
<INPUT TYPE="SUBMIT" VALUE="Send the Data1">
</FORM>
</BODY>
</HTML>
文本框的属性中,TYPE和NAME是必须的,其余是可选属性。SIZE属性用于设置文本框的可视大小;MAXLENGTH指明用户键入字符的最大长度;VALUE给出了一个最初显示在文本框中的值。
<input type="text" name="" size="" maxlength="" value="">
文本区域可以输入多行文本。NAME和ROWS属性是必须的。ROWS属性表明了文本区域内可以看到的文本行数,充满时会滚动。COLS属性指明可见文本列数与行数类似。WRAP属性指明文本区域内单词换行的方式,可以指定如下值。该标签为双标签。
值 | 说明 |
off | 禁止单词换行但用户可以输入换行符强制换行 |
virtual/soft | 各行显示为换行,但是换行并没有被发送到服务器 |
physica/hard | 启用了单词换行 |
<inputarea name="" rows="" cols="" wrap="">
创建密码框的语法与文本框相同,但要将TYPE属性指定为PASSWORD而不是TYPE。
<input type="password" name="" size="" maxlength="" value="">
取两个值中的一个,即二选一。TYPE属性是必须的,checked属性出现,该复选框默认情况会被选定。value属性指定复选框被选定情况下被发送到服务器的值,默认发送on值。法如下:
<input type="checkbox" name="" checked value="">
语法与复选框属性含义相同,但是TYPE属性的值必须是RADIO,NAME属性是必须的。
<input type="radio" name="" checked value="">
用户可以选择一个或者多个选项,它是一个滚动菜单。
<select name="" multipile size="">options go here</select>
name属性是必须的,multipile属性指明用户可以通过按下crtl键并单击多个选项来选择它们
列表框的单选行为可作为单选按钮。
<option selected value="text"></options>
<input type="hidden" name="text"value="">
<input type="FILE" name="name" accept="time" value="text">
其中type属性是必须的。格式通过使用MIME码指定。常用的格式如下:
超文本标记语言文本 .html,.html text/html
普通文本 :txt text/plain
word文档:application/msword
RTF文本 :rtf application/rtf
GIF图形 :gif image/gif
JPEG图形 :jpeg,
jpg: image/jpeg
au声音文件:au audio/basic
MIDI音乐文件 :mid,.midi audio/midi,audio/x-midi
RealAudio音乐文件 .ra, .ram audio/x-pn-realaudio
MPEG文件 .mpg,.mpeg video/mpeg
AVI文件 .avi video/x-msvideo
GZIP文件 .gz application/x-gzip
压缩文件.rar application/octet-stream
压缩文件.zip application/x-zip-compressed
TAR文件 .tar application/x-tar
<input type="image" src="url" name="text" align="align">
<input type="reset" value="text">
.引用相关头文件
引入CSS:
<link href="Scripts/jquery-ui-1.8.1.custom.css" rel="stylesheet" type="text/css" />
<link href="Scripts/ui.jqgrid.css" rel="stylesheet" type="text/css" />
引入JS:
<script src="Scripts/jquery-1.5.1.js" type="text/javascript"></script>
<script src="Scripts/jquery-ui.min.js" type="text/javascript"></script>
<script src="Scripts/grid.locale-en.js" type="text/javascript"></script>
<script src="Scripts/jquery.jqGrid.min.js" type="text/javascript"></script>
因为jqGrid3.6及以后的版本集成了jQuery UI,所以,此处需要导入UI相关js和css。另外grid.locale-en.js这个语言文件必须在jquery.jqGrid.min.js之前加载,否则会出问题。
2.将jqgrid加入页面中
根据jqGrid的文档,要想生成一个jqGrid,最直接的方法就是:
$("#list").jqGrid(options);
其中list是页面上的一个table:<table id="list"></table>
下面是一个简单的例子:
<script type="text/javascript">
$(document).ready(function () {
jQuery("#list").jqGrid({
url: 'Handler.ashx',
datatype: "json",
mtype: 'GET',
colNames: ['SalesReasonID', 'Name', 'ReasonType', 'ModifiedDate'],
colModel: [
{ name: 'SalesReasonID', index: 'SalesReasonID', width: 40, align: "left", editable: true },
{ name: 'Name', index: 'Name', width: 100, align: "center" },
{ name: 'ReasonType', index: 'ReasonType', width: 100, align: "center" },
{ name: 'ModifiedDate', index: 'ModifiedDate', width: 150, align: "center", search: false }
],
rowList: [10, 20, 30],
sortname: 'SalesReasonID',
viewrecords: true,
sortorder: "desc",
jsonReader: {
root: "griddata",
total: "totalpages",
page: "currpage",
records: "totalrecords",
repeatitems: false
},
pager: jQuery('#pager'),
rowNum: 5,
altclass: 'altRowsColour',
//width: 'auto',
width: '500',
height: 'auto',
caption: "DemoGrid"
}).navGrid('#pager', { add: true, edit: true, del: true,search:false,refresh:false }); ;
})
二、 jqgrid的重要选项
具体的options参考,可以访问jqGrid文档关于option的章节(http://www.trirand.com/jqgridwiki/doku.php?id=wiki:options)。其中有几个是比较常用的,重点介绍一下:
2.1 prmNames选项
prmNames是jqGrid的一个重要选项,用于设置jqGrid将要向Server传递的参数名称。其默认值为:
prmNames : {
page:"page", // 表示请求页码的参数名称
rows:"rows", // 表示请求行数的参数名称
sort: "sidx", // 表示用于排序的列名的参数名称
order: "sord", // 表示采用的排序方式的参数名称
search:"_search", // 表示是否是搜索请求的参数名称
nd:"nd", // 表示已经发送请求的次数的参数名称
id:"id", // 表示当在编辑数据模块中发送数据时,使用的id的名称
oper:"oper", // operation参数名称
editoper:"edit", // 当在edit模式中提交数据时,操作的名称
addoper:"add", // 当在add模式中提交数据时,操作的名称
deloper:"del", // 当在delete模式中提交数据时,操作的名称
subgridid:"id", // 当点击以载入数据到子表时,传递的数据名称
npage: null,
totalrows:"totalrows" // 表示需从Server得到总共多少行数据的参数名称,参见jqGrid选项中的rowTotal
}
2.2 jsonReader选项
jsonReader是jqGrid的一个重要选项,用于设置如何解析从Server端发回来的json数据,如果Server返回的是xml数据,则对应的使用xmlReader来解析。jsonReader的默认值为:
jsonReader : {
root: "rows", // json中代表实际模型数据的入口
page: "page", // json中代表当前页码的数据
total: "total", // json中代表页码总数的数据
records: "records", // json中代表数据行总数的数据
repeatitems: true, // 如果设为false,则jqGrid在解析json时,会根据name来搜索对应的数据元素(即可以json中元素可以不按顺序);而所使用的name是来自于colModel中的name设定。
cell: "cell",
id: "id",
userdata: "userdata",
subgrid: {
root:"rows",
repeatitems: true,
cell:"cell"
}
}
假如有下面一个json字符串:
{"totalpages":"3","currpage":"1","totalrecords":"11","griddata":[{"SalesReasonID":"1","Name":"Price","ReasonType":"Other","ModifiedDate":"1998年6月1日"},{"SalesReasonID":"2","Name":"On Promotion","ReasonType":"Promotion","ModifiedDate":"1998年6月1日"},{"SalesReasonID":"3","Name":"Magazine Advertisement","ReasonType":"Marketing","ModifiedDate":"1998年6月1日"},{"SalesReasonID":"4","Name":"Television Advertisement","ReasonType":"Marketing","ModifiedDate":"1998年6月1日"},{"SalesReasonID":"5","Name":"Manufacturer","ReasonType":"Other","ModifiedDate":"1998年6月1日"}]}
其对应的jsonReader为:jsonReader: {
root: "griddata",
total: "totalpages",
page: "currpage",
records: "totalrecords",
repeatitems: false
}
注:cell、id在repeatitems为true时可以用到,即每一个记录是由一对id和cell组合而成,即可以适用另一种json结构。援引文档中的例子:
repeatitems为true时:
jQuery("#gridid").jqGrid({
...
jsonReader : {
root:"invdata",
page: "currpage",
total: "totalpages",
records: "totalrecords"
},
...
});
json结构为:
{
"totalpages": "xxx",
"currpage": "yyy",
"totalrecords": "zzz",
"invdata" : [
{"id" :"1", "cell" :["cell11", "cell12", "cell13"]}, // cell中不需要各列的name,只要值就OK了,但是需要保持对应
{"id" :"2", "cell" :["cell21", "cell22", "cell23"]},
...
]
}
repeatitems为false时:
jQuery("#gridid").jqGrid({
...
jsonReader : {
root:"invdata",
page: "currpage",
total: "totalpages",
records: "totalrecords",
repeatitems: false,
id: "0"
},
...
});
json结构为:
{
"totalpages" : "xxx",
"currpage" : "yyy",
"totalrecords" : "zzz",
"invdata" : [
{"invid" : "1","invdate":"cell11", "amount" :"cell12", "tax" :"cell13", "total" :"1234", "note" :"somenote"}, // 数据中需要各列的name,但是可以不按列的顺序
{"invid" : "2","invdate":"cell21", "amount" :"cell22", "tax" :"cell23", "total" :"2345", "note" :"some note"},
...
]
}
2.3 colModel的重要选项
colModel也有许多非常重要的选项,在使用搜索、排序等方面都会用到。这里先只说说最基本的。
三、 注意事项
1. 动态改变Add Form或者Edit Form中的select的内容,如:改变下图中的Comparator下拉中的内容。
$("#list_d").navGrid('#pager_d',{add:true,edit:true,del:true,search:false,refresh:false},
{
checkOnSubmit:false, closeAfterEdit: true,recreateForm:true,
beforeInitData:function(formid){
initComparator();
},
beforeShowForm: function(formid){
$("#list_d").jqGrid('setColProp', 'Name', { editrules:{required:false},});
$('#tr_Name', formid).hide();
}
},//edit
{},//add
{}//del
)
beforeInitData, beforeShowForm在每次点击编辑的时候都会执行。initComparator的作用是通过ajax获取数据,然后利用$("#list_d").jqGrid('setColProp', 'Comparator', { editoptions: { value: valueString} });来设置Comparator下拉中的内容。其中valueString的格式如下’ equal to: equal to; not equal to: not equal to’。键值之间用冒号隔开,2项之间用分号隔开。注意:把recreateForm设为true,否则'setColProp'只在第一次调用时有效。
2. var rowNum = parseInt($(this).getGridParam("records"), 10); 得到数据条数。
3. jQuery("#list_d").clearGridData();清空数据。
4. jQuery("#list").getCell(ids,"Key");获取第ids行的key列。
5. $("#list").jqGrid('setSelection', "1");选中第一行。放在loadComplete:中在gird加载完成的时候自动选中第一行。loadComplete:function(data){$("#list").jqGrid('setSelection', "1");
}
6. 对于像1中的可编辑的字段,可以设定rule,参见http://www.trirand.com/jqgridwiki/doku.php?id=wiki:common_rules#editrules
7. 修改Option,以URL为例
jQuery("#list_d").jqGrid('setGridParam',{url:"xxx.aspx",page:1}).trigger('reloadGrid');
、html表单控件
<input type="radio" name="nw" value="1" title="内网" checked="" runat="server">
<input type="radio" name="nw" value="2" title="外网" runat="server">
获取值:
*请认真填写需求信息,我们会在24小时内与您取得联系。