使用cgiFormString可以获取请求参数中指定name的值
乱码问题:地址栏中直接输入QueryString,一般浏览器只对中文字符进行utf8编码,而cgic属于支持ansi编码。
ContentType不要忘了写charset=gbk,否则可能会有乱码问题。
//http的报文头
cgiHeaderContentType("text/html;charser=gbk");
char name[256] = { 0 };
char pass[256] = { 0 };
if (strlen(name) <= 0) {
fprintf(cgiOut, "<script>alert('请输入用户名')</script>");
}
if (strlen(pass) <= 0) {
fprintf(cgiOut, "<script>alert('请输入密码')</script>");
}
cgiFormString("name", name,sizeof(name));
cgiFormString("pass", pass, sizeof(pass));
fprintf(cgiOut, "<html><head></head><body>name=%s,pass=%s</body></html>",name,
另外一种写法:
//http的报文头
cgiHeaderContentType("text/html;charser=gbk");
char name[256] = { 0 };
char pass[256] = { 0 };
if (cgiFormString("name", name, sizeof(name)) != cgiFormSuccess) {
fprintf(cgiOut, "<script>alert('请输入用户名')</script>");
};
cgiFormString("pass", pass, sizeof(pass));
fprintf(cgiOut, "<html><head></head><body>name=%s,pass=%s</body></html>",name,pass);
cgiHeaderLocation
重定项,其实就是简化了printf("Location:");
表单提交
<html>表单可以自动给服务器提交参数,get是通过url,post是通过报文题,不用用户自己拼url,action指定把表单内容提交给谁。
浏览器向服务器端提交数据,被提交数据的表单放到form中,form中通过action属性设定表单被提交给那个页面,为了在服务端读取出表单的值,需要在html中为表单元素设定name属性。
注意id是给js操作dom用的,name才是提交给服务器用的,id不能重复,name可以重复,重复的name的值都会被提交给服务器。
服务器端用cgiFormString等来根据表单项的name来获得提交的属性值。
checkbox没选中为null,选中为on
强调:登录是浏览器提取出来值发送给服务器,不是服务器读取用户填写的这个页面。
将用户填写的内容提交到服务器有如下几个条件。
只能为input,textarea,select三种类型的标签的value属性的值(select是选中项的value)才会提交给服务器。
标签必须设定name属性,道理,没有name怎么拼网址。
对于radiobutton,同name的为一组,选中的radiobutton的value被提交到服务器。
form外的input标签被忽略。