注册 登录
编程论坛 ASP技术论坛

一个关于动态checkbox取值写入数据库问题(完美解决),谢谢各位!

nicechlk 发布于 2010-04-22 23:55, 4795 次点击
有一动态checkbox作如下循环:
<%do while not rs.eof%>
<input type="checkbox" name="info" value="yes" <%if rs("state")=true then response.write "checked" end if%>><%=rs("in_name")%>
<%rs.movenext
next%>

其中,字段state是布尔型,表示是否选中;字段in_name是项目名称。
请教如何获取点选过的checkbox值,同时将值(true/false)写入到state字段?

[ 本帖最后由 nicechlk 于 2010-4-24 21:14 编辑 ]
19 回复
#2
gupiao1752010-04-23 09:59
<form action="?act=ckk" name="from1" id="form1" method="post">
<%for i=1 to 5%>
<input type="checkbox" name="info" id="info" value="<%=i%>"><%=i%>
<%next%>
<input type="submit" value="tijiao">
</form>
<%
if request.queryString("act")="ckk" then
dim ck
ck=split(request.form("info"),",")
for j=0 to ubound(ck)
response.write ck(j)
//这里写字段操作,CHECKBOX的VALUE值已经出来,可以根据这个ID去找到数据库里的ID,并把对应ID的STATE改为TRUE就好了!
next
end if
%>
要哪个取哪个!
#3
nicechlk2010-04-23 12:05
楼上的方法很好,赞一个。
不过,这个id取出来写不进数据库,不知为何?
conn.execute ("update info_product set info_choice=true where id="&i)
#4
gupiao1752010-04-23 12:52
多谢夸奖!你就是那个叫人把显示器烧了,然后获得页面变灰的家伙吧!

写不进数据库?有没有错误提示?
建议你在执行 conn.execute ("update info_product set info_choice=true where id="& i) 以前先输出i看看,是否有值,并注意数据格式(字符型变成整型),你这么聪明,一定可以很快解决问题的!
#5
nicechlk2010-04-23 13:29
嘿嘿。。。烧显示器这事还让你记在心上,实在不好意思。

没有错误提示,代码如下:
dim action,var,var1
action=request.QueryString("action")
if action="ti" then   
    var=request.Form("info")
    var1=split(var,",")
    var1=cint(var1)    '转换数字型;   
        for i=0 to ubound(var1)
            response.Write var1(i)   '输出正确;
            conn.execute ("update info_product set info_choice=true where id="&i)    '所有值都写入了true,貌似where没用;
        next
end if
#6
gupiao1752010-04-23 13:45
有值输出,说明前面没有问题,怎么会更新不了呢?
推测:
1、可能是SQL语句出问题了,info_choice这个字段是-是/否字段吗?
2、打开数据库,在里面用查询方式,单独执行更新看看,看能否正确执行!如果可以正确执行,就得考虑是否SQL执行方式的问题了!
3、你的ASP页面是否使用或包含(include)了这样的语句:on error resume next,如果有,就把它暂时删除,否则即使有问题也不会报错的!
#7
zzy_4202010-04-23 14:31

改成如下:主要是单引号的问题:

dim action,var,var1
action=request.QueryString("action")
if action="ti" then   
    var=request.Form("info")
    var1=split(var,",")
    var1=cint(var1)    '转换数字型;   
        for i=0 to ubound(var1)
            response.Write var1(i)   '输出正确;
            conn.execute ("update info_product set info_choice=true where id='" & i &"'") '这里的引号加上.            
      next
end if

[ 本帖最后由 zzy_420 于 2010-4-24 10:50 编辑 ]
#8
nicechlk2010-04-24 12:02
dim action,var,var1
action=request.QueryString("action")
if action="ti" then   
    var=request.Form("info")
    var1=split(var,",")
    var1=cint(var1)    '转换数字型;   
        for i=0 to ubound(var1)
            response.Write var1(i)   '输出正确;
            conn.execute ("update info_product set info_choice=true where id="&i)    '所有值都写成了true,貌似where没起作用;
            conn.execute ("update info_product set info_choice=true where id="&var1(i))   '故障依然
            conn.execute ("update info_product set info_choice=true where id='"&i&"'")   '故障依然
            conn.execute ("update info_product set info_choice=true where id='"&var1(i)&"'")   '故障依然
            sql="select * from info_product where id="  '以上四种方式作为条件
            rs.open sql,conn,1,3
            rs("info_choice")=true
            rs.update            '故障依然;

        next
end if
崩溃中。。。。。
执行中没有任何错误提示,info_choice字段为布尔型,运行环境win7 旗舰版(貌似跟系统关系不大吧?)

[ 本帖最后由 nicechlk 于 2010-4-24 12:04 编辑 ]
#9
yms1232010-04-24 12:29
获得所有选中的checkbox?
<script language="javascript">
function Submit_Click()
{
   var selInfo=getSelectedInfo();
   if(selInfo=="")
   {
      alert('你没有选中任何一项,请至少选择一项');
      return false;
   }
   selInfo=selInfo.replace("|","\n");
   alert('选择的项目有'+selInfo+"\n");
}
//获得选中的Info信息
function getSelectedInfo()
{
   if(this.info.length==null)//判断info是否有多个
   {
       //判断info是否选中
       if(this.info.checked)
          return this.info.value;//返回选中的value
       return ""; //未选中返回空
   }
   //多个info的取值循环
   var reVal="";//value字符串临时变量
   for(var i=0;i<this.info.length;i++)
   {
       //判断其中一个是否选中
       if(this.info[i].checked)
          reVal+=this.info[i].value+"|";//选中加入临时变量
   }
   return reVal;//返回临时变量
}
</script>
<%do while not rs.eof%>
<input type="checkbox" name="info" value="yes" <%if rs("state")=true then response.write "checked" end if%>><%=rs("in_name")%>
<%rs.movenext
next%>
<input type="button" name="Submit" onClick="Submit_Click();" value="提交">
#10
nicechlk2010-04-24 14:27
你好yms,十分感谢!
我对js懂得不多,看的头大
如果不用js方法能行得通不?
#11
yms1232010-04-24 15:03
个人习惯用客户端JS+服务器端代码来实现,有时候某些功能就是需要JS和服务器代码联合实现。
#12
nicechlk2010-04-24 18:09
恩,很多时候用js实现起来的确很方便,也很强大。
可是js的代码也的确绞人,尤其新手

另外,2楼的思路也是很可取的,就是到最后一步行不通了,不知为何?本着技术是严谨的这个态度让我整理一下思路:
1,首先,通过rs.open "select * from info_product",conn,1,1 查询,
    <%if not (rs.eof and rs.bof) then
        for i=1 to rs.recordcount
            if rs.eof then
                exit for
            end if
            response.write rs("info_name")%>
    <input type="checkbox" name="info" value="<%=i%>">
    <%rs.movenext
        next
        rs.close
    end if%>   '到这儿是没问题的,一共有6条记录。
<input type="submit" value="确认">
2,然后对获取checkbox的值,并作处理。
<%dim var,var1
var=request.form("info")
var1=split(var,",")
for i=0 to ubound(var1)
    response.write var1(i)  '测试结果是:如果全选,则1 2 3 4 5 6,或者部分,1 3 5 ,这也是没问题的,
    set rs=conn.execute ("select * from info_product where id="&var1(i))   '此处做测试,用条件查询;
        do while not rs.eof        
            response.write rs("id") "&" rs("info_name")
        rs.movenext
        loop         '测试成功,可以按照此条件查询到结果;
        rs.close
    conn.execute ("update info_product set info_choice=true where id="&var1(i))  '为何全部都更新为true?并未按条件执行。
next%>

[ 本帖最后由 nicechlk 于 2010-4-24 19:36 编辑 ]
#13
nicechlk2010-04-24 19:36
不好意思朋友们,问题基本解决。
其实上面所述之方法是可行的,更新其实是对的。
其原因如下:
conn.execute ("update info_product set info_choice=true where id="&var1(i))  这条语句只更新点选过的checkbox,未选的并没有做改变,因此,需要在for循环头部先清空所有info_choice的值,即conn.execute("update info_product set into_choice=false"),之后再执行更新就可以了。
另外,在点击确定之后,调用了form表单的action=" ?action=ti"执行的,不知为何checkbox所在的form表单并未刷新,从而造成错觉,以为更新无效。。。真是郁闷。
正琢磨如何在点击确定之后能让页面刷新。。。。


[ 本帖最后由 nicechlk 于 2010-4-24 19:38 编辑 ]
#14
nicechlk2010-04-24 21:21
2,然后对获取checkbox的值,并作处理。
<%dim var,var1
var=request.form("info")
var1=split(var,",")
conn.execute ("update info_product set info_choice=flase) '先执行所有值均为否的操作;
for i=0 to ubound(var1)
    conn.execute ("update info_product set info_choice=true where id="&var1(i))
next
response.write "<script>alert('操作成功!');location.href='product_ClassManager.asp';</script>" '给个提示同时刷新页面;
%>
至此,问题完美解决,谢谢各位兄弟!同时给依然有此问题的朋友做一参考。
#15
nicechlk2010-04-24 21:23
2,然后对获取checkbox的值,并作处理。
<%dim var,var1
var=request.form("info")
var1=split(var,",")
conn.execute ("update info_product set info_choice=flase) '先执行所有值均为否的操作;
for i=0 to ubound(var1)
    conn.execute ("update info_product set info_choice=true where id="&var1(i))
next
response.write "<script>alert('操作成功!');location.href='product_ClassManager.asp';</script>" '给个提示同时刷新页面;
%>
至此,问题完美解决,谢谢各位兄弟!同时给依然有此问题的朋友做一参考。
#16
nicechlk2010-04-24 21:24
咋提交了2遍?
#17
gupiao1752010-04-25 19:58
以下是引用nicechlk在2010-4-23 13:29:17的发言:

嘿嘿。。。烧显示器这事还让你记在心上,实在不好意思。

没有错误提示,代码如下:
dim action,var,var1
action=request.QueryString("action")
if action="ti" then   
    var=request.Form("info")
    var1=split(var,",")
    var1=cint(var1)    '转换数字型;   
        for i=0 to ubound(var1)
            response.Write var1(i)   '输出正确;
            conn.execute ("update info_product set info_choice=true where id="&i)    '所有值都写入了true,貌似where没用;
        next
end if
搂主的研究细节的精神非常不错,值得我学习!哪怕问题解决了依旧还做一个总结和归纳!
我想问问:
  conn.execute ("update info_product set info_choice=true where id="& amp;i)    '所有值都写入了true,貌似where没用;
这句是否一开始其实就可以执行的或者说已经执行了,只是视觉上你认为他们没有写入数据库??晕了!
#18
gupiao1752010-04-25 20:01
另外给楼主一个建议,如果准备走WEB的道路。我建议JAVASCRIPT一定要学,哪怕你以后不做WEB,改做其他软件,其学习思想也是可以借鉴的!其语法很接近JAVA,有面向对象语言的特性!更像是缩减灵活版的JAVA。所以一定要学,推荐看图灵版的<<精通JAVASCRIPT>>!
#19
gupiao1752010-04-25 20:38
根据斑竹YMS123的JS代码,我也改写了一个JS取CHECKBOX值的方法!类似代码如下:
<script language="javascript">
function Submit_Click()
{
var info=document.getElementsByName("info");
var reVal="";
  for(var i=0;i<info.length;i++)
   {     if(info[i].checked)
         reVal+=info[i].value+"|";//选中加入临时变量
   }
   var fz=reVal.split("|");
   for (var j=0;j<reVal.length;j++)
   {
  var dd=parseInt(fz[j]);
  if(!isNaN(dd))
   {alert(dd);}//这里获得的就是取得的已选中的checkbox的VALUE值,可以直接处理的数据,传入AJAX或者拿去干什么都可以!
   }
}
</script>

<input type="checkbox" name="info" value="1">1
<input type="checkbox" name="info" value="2">2
<input type="checkbox" name="info" value="3">3
<input type="checkbox" name="info" value="4">4
<input type="checkbox" name="info" value="5">5
<input type="button" name="Submit" onClick="Submit_Click();" value="提交">
#20
nicechlk2010-04-25 22:23
回复 17楼 gupiao175
conn.execute ("update info_product set info_choice=true where id="&i   '此条语句造成更新均为true,条件where不起作用;
conn.execute ("update info_product set info_choice=true where id="&var1(i)   '这样可以。只因提交后,页面未更新执行过的结果,造成假象。因此才想到用<script>.....</script>来刷新页面,如此以来,便可及时看到结果。
谢谢你的关注。
1