Avatar of Saroj13
Saroj13

asked on 

Insert multiple selected rows in one query in database using Ajax, javscript and sql server 2005

Hi,

I am dynamically generating the checkboxes with urls.
<input type="checkbox" value="100,1,john, date1" name="doc"/><a href="100">Cnn</a><br />
<input type="checkbox" value="105,1,john, dat21" name="doc"/><a href="105">MSN</a><br />
<input type="checkbox" value="109,3,john, date1" name="doc"/><a href="109">FOX</a><br />
<input type="checkbox" value="300,1,john, date1" name="doc"/><a href="300">NBBC</a><br />
<input type="checkbox" value="900,4,john, date1" name="doc"/><a href="900">MSDN</a><br />

There can be any number of checkboxes. Also, user can select one checkbox , they may select multiple checkboxes.

<input type="submit" name="btnsubmit" value="insert" onclick="return check_value()" />.

With the below code, I can able to insert single selected checkbox values in database.

PROBLEM: how to insert multiple selected checkbox values  in sql server?
I have 4 columns in one table dname, dlabel, uid, date. I want to insert all selected values in table together at one submit. How I can do that.

My sql insert query
INSERT INTO DocTable
            (
                  dName   , dLabel,
                  UID     , Date  
            )
      VALUES
            (
                  @dName   ,    @dLabel,
                  @UID     ,      @Date  
            )



Ajax code for insert data and getting the selected checkbox values
function check_value()
{
 
var c_value = "";
 var dinfo ="";
 var dname =""; 
 var dLabel ="";
 var uid="";
 var Date="";
 
var count = document.form.Bulletin.length;
for (var i=0; i < document.form.doc.length; i++)
   {
   if (document.form.doc[i].checked)
      {
      c_value = c_value + document.form.doc[i].value + "\n";
      alert(c_value);
     
     dinfo = c_value.split(",");
     dname= dinfo[0];
     dLabel = dinfo[1];
     uid = dinfo[2];
     Date = dinfo[3];
      
      }
   }
   
    if(count > 0) 
  		{	
   xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
   var WebServiceurl = "/rWebService/test.asmx/Insertdocn";
   var parameters = "sdName=" + encodeURIComponent(dname) + "&sLabel="+encodeURIComponent(dLabel) + "&sUD="+encodeURIComponent(uid) + "&CurrentDate="+encodeURIComponent(Date);
   xmlHttp.open("POST", WebServiceurl, true);
   xmlHttp.onreadystatechange = doUpdate;
   xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
   xmlHttp.send(parameters);
 return false;
   }
}

Open in new window

JavaScriptAJAXMicrosoft SQL Server 2005

Avatar of undefined
Last Comment
Saroj13

8/22/2022 - Mon