Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Insert Records into SQL Server Database via JS Button

Status
Not open for further replies.

LilProgrammerGirl

Programmer
Jun 24, 2004
81
US
Hi everyone,
I have a button that when you click on it.. I would like it to insert a record (variables from page) into a database on my SQL Server. How would I go about doing this?

Here is my code for the button:

Code:
<input type="submit" value="Skip W/O" name="B3"  onclick="return SkipCheck();">
and the code for the function I started:
Code:
function SkipCheck() {
 alert("Nothing is programmed on the Skip Button Yet.");
 return false;
}

Any advise would be greatly appreciated!

Thanks much!
Hailey
 
what are you using to connect to the DB? Javascript?



[cheers]
Cheers!
Laura
 
In the page above I connected via ASP to display records from the database. Then, I closed the connection. At the bottom of the screen I have the "Skip W/O" button - all of the variables that I pulled from the database before, I assigned to variables. So, my plan was to write a javascript function to reconnect to the database and insert a record with the variables into a new table. I would like to use Javascript because I have 4 buttons that do differnt things, therefore not allowing me to do the connection on the form submit. Can I use my ASP Code in my JS Function?? How??

Here's my ASP Code for my connection:

Code:
<% 
Dim ConStr, RS, SQL, conn
                  
strPickSup = Request.form("Supervisor")
session("PickSup") = strPickSup
                  
set conn=Server.CreateObject("ADODB.Connection")
conn.ConnectionTimeout=120
CONN.OPEN "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=WOA;Data Source=MYSERVER"

Set RS = Server.CreateObject("ADODB.RecordSet")
SQL = "select top 1 A.account_number, A.principal_name, A.Subscriber_id, A.Tax_ID, A.wo_seq_number, A.date_entered, A.install_date, A.time_frame, A.workorder_type, A.work_units, A.orig_oper_id, A.salesrep, A.campaign_code, A.reason_code, A.descr, A.comments from Tbl_WOA A, T_Employee B where A.salesrep = B.salesrep and B.Sup = '" & strPickSup & "' order by a.date_entered, a.principal_name;"

RS.open SQL, conn, 1, 2
%>
 
Okay, so I've gotten as far as this, but I still need help! I don't know if I am using the right connection and also where to put the parenthesis.. Can anyone help out?

Thank you!!!

Code:
function SkipCheck() {

 sql="INSERT INTO Tbl_History "
 sql=sql + "(account_number, name"
 sql=sql + " VALUES "
 sql=sql + "('1234','Hailey')"

  document.write(set conn=Server.CreateObject("ADODB.Connection"))
  document.write(conn.ConnectionTimeout=120)
  document.write(CONN.OPEN "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=MYDB;Data Source=MYSERVER")
  document.write(Set RS = Server.CreateObject("ADODB.RecordSet"))
  document.write(CONN.execute(sql))
  document.write(RS.close)
  document.write(set RS = nothing)
  document.write(conn.close)
  document.write(set conn = nothing)

 alert(sql);
 return false;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top