coldfusion批量保存

  最近一直在学习coldfusion,今天遇到了一个动态增行,并进行批量保存的问题,搞了半天终于搞定啦,发出来和大家分享一下。

  以下是具体代码,感兴趣的朋友可以看看:

<cfprocessingdirective pageencoding="utf-8">
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="webroot/js/jquery.js"></script>
<script>
	function addCustomField(id){
		val = parseInt(Math.random()*100);
		tr_id = $("#test>tbody>tr:last").attr("id");
        tr_id++;
        str = "<tr><td><input type='text' name='username_"+val+"'/><input type='password' name='userpass_"+val+"' /></td></tr>";
        $('#'+id).append(str);
	}
</script>
<cfif isDefined("form.submit")>
	<cfif isdefined("form.fieldnames")>
       <cfif form.fieldNames CONTAINS "username_">
          <cfloop list="#form.fieldnames#" index="i">
			<cfif i CONTAINS "username_">
               <cfset fieldid = listRest(i,"_")>
                   <cfquery name="batch" datasource="#request.dsn#">
                      insert into cf_user(name,pass) 
                      values(
                      	'#evaluate("form.username_" & fieldid)#',
                        '#evaluate("form.userpass_" & fieldid)#'
                      )
                   </cfquery>
             </cfif>
         </cfloop>
      </cfif>
   </cfif>
</cfif>
<title>无标题文档</title>
</head>

<body>
<cfform>
	<table id="cust_table">
    	<tr>
        	<td>用户注册</td>
        </tr>
        <tr>
        	<td>
            <input type="text" name="username_1" />
            <input type="password" name="userpass_1" />
            </td>
            <td><img src="images/admin_add.jpg" onclick="addCustomField('cust_table');"/></td>
        </tr>
        <input type="submit" name="submit" value="注册" />
    </table>

</cfform>
</body>
</html>

 数据库当然还是最简单的用户表,只有三个字段:

create table cf_user(
  id int primary key auto_increment,
  name varchar(20),
  pass varchar(20)
)

 以上就是全部的信息啦,感兴趣的朋友可以一起交流coldfusion的信息,本人QQ:395079181

你可能感兴趣的:(html,jquery,qq,XHTML,ColdFusion)